Help with multithreading

90 views
Skip to first unread message

mcot

unread,
Mar 9, 2011, 5:42:57 PM3/9/11
to PyV8
I would like to implement the code shown below in PyV8. Obviously the
code I have won't work, but I think the idea that I want to achieve is
clear. I am just wondering about a little clarification on how the v8/
pyv8 threading/lock system works and how best to implement the idea
using those constructs. Thanks in advance.

# Thread Testing

import threading
import time
import PyV8

class Global(PyV8.JSClass):

def __init__(self):
context = None

def runCode(self, codeString):
self.context.eval(codeString)

def setCode(self, codeString, callbackDelay):
th = threading.Timer( callbackDelay, self.runCode,
(codeString,) )
th.start()


g = Global()
with PyV8.JSContext(g) as ctx:

g.context = ctx
ctx.eval("""
setCode("testVal = 10", 1);
""")
time.sleep(2)

# I would like this to print 10
print ctx.locals.testVal

Flier Lu

unread,
Mar 28, 2011, 1:24:00 PM3/28/11
to PyV8
The original multi-thread support in v8 is limited, it use a global
lock to execute script in same thread one by one. It means, if you
want to allow another thread to execute script, you must first unlock
the current thread with PyV8.JSUnlocker and lock the new thread with
PyV8.JSLocker. Please check the testMultiJavascriptThread in PyV8.py

The good news is the latest V8 trunk code has added a new Isolated
concept, which allow us execute script in different script.

I'm still working on it, and design a new mechanism to simplify the
multi-thread support.

/**
* Isolate represents an isolated instance of the V8 engine. V8
* isolates have completely separate states. Objects from one isolate
* must not be used in other isolates. When V8 is initialized a
* default isolate is implicitly created and entered. The embedder
* can create additional isolates and use them in parallel in multiple
* threads. An isolate can be entered by at most one thread at any
* given time. The Locker/Unlocker API can be used to synchronize.
*/
class V8EXPORT Isolate {
Reply all
Reply to author
Forward
0 new messages