1

I made a simple offscreen renderer with cefpython.

I use cefpython.MessageLoop(), and I have a separate thread to do something every seconds:

[... cefpython initialization ...]

startTime = time.time()

def main_loop():
    print time.time() - startTime
    threading.Timer(1, main_loop).start()

main_loop()
cefpython.MessageLoop()

This works fine, but it slows down after 30 seconds (then the loop updates every 10 seconds).

This is the output that I get:

[CEF Python] Initialize() called
[CEF Python] CefExecuteProcess(): exitCode = -1
[CEF Python] CefInitialize()
[...]
3.11049604416
4.11594009399
5.11900210381
[...]
27.2121961117
28.2259521484
29.2369601727
[... then it updates every 10 seconds ...]
40.1490521431
50.1502101421
60.1521630287

Strangely, the loop also updates when I switch focus from my terminal window to google chrome (but not when I switch from my terminal to other apps).

--- Update ---

The loop runs properly if I give focus on the python app (accessible from python's rocket icon on the OS X dock). I was confused because the loop does not updates properly when I give focus to the terminal with which I launched the python script.

arthur.sw
  • 11,052
  • 9
  • 47
  • 104
  • 1
    Generally Python has poor support for threading, read about GIL (global interpreter lock). In CEF there is PostTaskDelayed() that allows to post tasks on specific threads with a timed delay, but this function wasn't yet exposed in cefpython. – Czarek Tomczak Mar 26 '15 at 09:20
  • So you mean there is no solution? Maybe I could use `MessageLoopWork` in my thread instead ([see here](http://stackoverflow.com/q/29238431/719276))? I don't understand how could `PostTaskDelayed()` help me with this problem? – arthur.sw Mar 26 '15 at 09:27
  • 1
    Have you tried calling PostTask() in your thread? (probably a bad idea, not optimized). Try also sched.scheduler to call MessageLoopWork. – Czarek Tomczak Mar 27 '15 at 09:21
  • I thought about using a scheduler (or just `time.sleep()`) but I am wondering whether `time.sleep()` is blocking thus suboptimal? – arthur.sw Mar 27 '15 at 10:15
  • With a `scheduler` recursively calling itself as I do with the thread, I get `maximum recursion depth exceeded while calling a Python object`. – arthur.sw Mar 27 '15 at 10:31
  • With a `while True` loop and `time.sleep(0.01)` it's even worse than with a separate thread: after 30 seconds it only updates after 2.5 seconds... (it correctly updates every 0.01 seconds before) – arthur.sw Mar 27 '15 at 10:34
  • I noticed that when I give the focus on the python app (not the terminal but the python app accessible from the rocket icon in the dock in os x) the update loop works properly (it does update every seconds, as expected). – arthur.sw Mar 27 '15 at 14:47
  • It seems that running the script from a `virtualenv` also fixes the problem. – arthur.sw Mar 27 '15 at 15:00

0 Answers0