简体   繁体   English

线程中的PyV8 - 如何使其工作?

[英]PyV8 in threads - how to make it work?

Using PyV8 in the main program is ok. 在主程序中使用PyV8是可以的。
But even if I run it in 1 thread (not main program itself, but 1 additional thread in it) 但即使我在1个线程中运行它(不是主程序本身,而是在其中添加1个线程)

class TaskThread(threading.Thread):
    def __init__(self, task):
        threading.Thread.__init__(self)
        self.task = task

    def run(self):
        try:
            self.task.run()
        except Exception as e:
            pass

a single line self.task.run() which has this code: 单行self.task.run() ,它有以下代码:

context = PyV8.JSContext(self.window)     # <- this stops everything

freezes the whole program. 冻结整个程序。

I already understood that PyV8 "doesn't like" threads, but still how can I use it for threaded tasks? 我已经知道PyV8“不喜欢”线程,但我仍然可以如何将它用于线程任务?

I think I found a solution while looking in http://code.google.com/p/pyv8/source/browse/trunk/PyV8.py 我想我在查看http://code.google.com/p/pyv8/source/browse/trunk/PyV8.py时找到了解决方案

If you start thread like: 如果您启动线程如:

t = YourThreadClass()
t.daemon = True
t.start()

just start it in this way: 以这种方式启动它:

with PyV8.JSLocker():
    t.start()

when you need to run javascript: 当你需要运行javascript时:

with PyV8.JSLocker():
    self.context.enter()
    print self.context.eval('1+1')
    self.context.leave()

Looks like it solves the problem. 看起来它解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM