-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a thread safe context api like py-mini-racer #100
Comments
Can you please describe a scenario where this happens? Moreover providing some samples would be great so that I could take a look at them. Thanks! |
Thanks for showing interest! I was trying to use it for SSR in a framework that i built called picomet. In this(https://github.com/picomet/picomet/blob/main/src/picomet/transformer.py) file you can see how the MiniRacer context class is being used. |
Sorry but I still can not understand the issue. If you need to use the JSContext instance multiple times you should really instantiate and use it every time you need it. Be aware that when you use the with statement, the JSContext is created and entered. When you're done the JSContext is exited and destroyed. So the following code
does not preserve the created JSContext instance. If you need to do that just use the following code
Hope it helps! |
Thanks. 😅
This is what I needed. Now I can persist data and retrieve them later when I need. But unfortunately it's not thread safe currently. Can it be made thread safe? |
Have you already taken a look at https://github.com/cloudflare/stpyv8/blob/master/tests/test_Thread.py? Let me know if that helps. |
This doesn't work. I don't know if i am doing something wrong. Or maybe it's not possible. import threading
import STPyV8
import time
class Global:
count = 0
started = threading.Event()
finished = threading.Semaphore(0)
def sleep(self, ms):
time.sleep(ms / 1000.0)
self.count += 1
g = Global()
def run():
class Transformer:
def __init__(self, *args, **kwargs):
with STPyV8.JSIsolate():
self.ctx = STPyV8.JSContext(g)
def store(self):
with STPyV8.JSIsolate():
with self.ctx as ctx:
ctx.eval(
"""
v = "hello world";
"""
)
def print(self):
with STPyV8.JSIsolate():
with self.ctx as ctx:
print(ctx.eval("v"))
transformer = Transformer()
transformer.store()
transformer.print()
t = threading.Thread(target=run)
t.start() |
Seems like you are using different isolates for each operation and that's not correct. Please give this code a try. This should help you figure out how to proceed.
|
That worked 🙂. Thanks for the help and the library. |
In some cases the
with
api doesn't work. You need to store the context in an object.The py-mini-racer context class(https://github.com/sqreen/PyMiniRacer/blob/f7b9da0d4987ca7d1982af7f24da423f06da9894/py_mini_racer/py_mini_racer.py#L163).
The text was updated successfully, but these errors were encountered: