2

I made a simple offscreen renderer with cefpython.

I used cefpython.MessageLoop() but I would like to execute a javascript function with browser.GetFocusedFrame().ExecuteFunctionwhich must be called from main UI thread.

Is there a way to set a callback on cefpython's message loop?

Alternatively I could use MessageLoopWork, but I don't know how. I tried to call it in a separate thread but it does not work:

import threading

def main_loop():
    cefpython.MessageLoopWork()
    threading.Timer(0.01, main_loop).start()

threading.Timer(0.01, main_loop).start()

I get the following error:

[0324/174806:ERROR_REPORT:context.cc(146)] Check failed: false. called on invalid thread
arthur.sw
  • 11,052
  • 9
  • 47
  • 104

1 Answers1

1

Use the cefpython.PostTask() function to post tasks on various CEF threads. See: https://code.google.com/p/cefpython/wiki/cefpython#PostTask_(int_threadId,_object_func_[,args..])_(void) and Issue 61. It's available since version 31.0.

The wxpython.py example shows how to use both timer with MessageLoopWork() and MessageLoop().

Czarek Tomczak
  • 20,079
  • 5
  • 49
  • 56
  • 1
    Thanks, I saw this function but I don't understand how to use it (I could not find it in wxpython.py). – arthur.sw Mar 24 '15 at 17:27
  • 1
    @arthur.sw See https://code.google.com/p/cefpython/source/browse/cefpython/cef3/windows/binaries_32bit/wxpython.py?r=5cf79c6eec11#657 – Czarek Tomczak Mar 24 '15 at 17:31
  • I successfully implemented your solution, but now I have [this problem](http://stackoverflow.com/q/29255630/719276), is this related? – arthur.sw Mar 25 '15 at 12:27
  • How can I use `cefpython.MessageLoopWork()` without any heavy GUI framework? – arthur.sw Mar 26 '15 at 11:06
  • @arthur.sw Another option worth trying is ApplicationSettings.multi_threaded_message_loop. See https://groups.google.com/d/msg/cefpython/jff7u75Fz7c/Ys_K00xyXoYJ – Czarek Tomczak Mar 27 '15 at 09:13
  • The problem is that I'm working on os x and it seems [not possible](https://code.google.com/p/chromiumembedded/issues/detail?id=745) – arthur.sw Mar 27 '15 at 10:06
  • @arthur.sw How about using Tkinter? It's a lightweight GUI framework I think and comes with python by default. See http://stackoverflow.com/a/2401181/623622 – Czarek Tomczak Mar 27 '15 at 10:21
  • @arthur.sw See also PyGUI, lightweight as well: http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ – Czarek Tomczak Mar 27 '15 at 10:24
  • Thanks for those tips, I'm working on it. Do you know how are coded the update loop of those frameworks? This is the only thing I need ;-) – arthur.sw Mar 27 '15 at 10:50
  • I get a `[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe958d6c970` when using `Tkinter` along with `cefpython`, then it stops :-/ – arthur.sw Mar 27 '15 at 10:59
  • @arthur.sw Are you calling cefpython.Initialize()? I recall similar error, see this issue https://code.google.com/p/cefpython/issues/detail?id=156 . For more cefpython support please use the CEF Python Forum. – Czarek Tomczak Mar 27 '15 at 11:07