[Jython-dev] Is jython thread safe?

Hi all,
Can anyone tell me if the PythonInterpreter is thread safe? That is, can two
thread simultaneously call into the interpreter and evaluate different
scripts? Are there any operations that are not thread safe and need to be
synchronized by the caller?
I have been searching all through the jython docs and googling like mad, but
I can't seem to find an authoritative answer to this question. Maybe if
someone on this mailing list knows, he or she can create a short article in
the Jython wiki addressing this question.
I am embedding Jython into a servlet container (tomcat) and am trying to
figure out whether I can use a single interpreter for all requests or I need
to (a) put a big fat lock around the interpreter or (b) create an
interpreter per request.
As a point of reference, JRuby does an excellent job of describing the
thread safety properties of their interpreter:
http://kenai.com/projects/jruby/pages/RedBridge
--matt

Thread view

Hi all,
Can anyone tell me if the PythonInterpreter is thread safe? That is, can two
thread simultaneously call into the interpreter and evaluate different
scripts? Are there any operations that are not thread safe and need to be
synchronized by the caller?
I have been searching all through the jython docs and googling like mad, but
I can't seem to find an authoritative answer to this question. Maybe if
someone on this mailing list knows, he or she can create a short article in
the Jython wiki addressing this question.
I am embedding Jython into a servlet container (tomcat) and am trying to
figure out whether I can use a single interpreter for all requests or I need
to (a) put a big fat lock around the interpreter or (b) create an
interpreter per request.
As a point of reference, JRuby does an excellent job of describing the
thread safety properties of their interpreter:
http://kenai.com/projects/jruby/pages/RedBridge
--matt

[Matt]
> Can anyone tell me if the PythonInterpreter is thread safe?
PythonInterpreter is thread-safe.
> That is, can two
> thread simultaneously call into the interpreter and evaluate different
> scripts? Are there any operations that are not thread safe and need to be
> synchronized by the caller?
But the exec and execfile methods of PythonInterpreter are not
thread-safe, so you need to synchronize them when
executing/evaluating.
> I have been searching all through the jython docs and googling like mad, but
> I can't seem to find an authoritative answer to this question. Maybe if
> someone on this mailing list knows, he or she can create a short article in
> the Jython wiki addressing this question.
That's a good idea. Probably a link to the relevant chapter of the
jython book would be a good idea.
> I am embedding Jython into a servlet container (tomcat) and am trying to
> figure out whether I can use a single interpreter for all requests
Yes you can use the same interpreter for all requests: see modjy for
an example of this.
[As an aside, is there a reason why you don't want to use modjy?]
> or I need
> to (a) put a big fat lock around the interpreter or (b) create an
> interpreter per request.
No, you don't have to do any do those things.
> As a point of reference, JRuby does an excellent job of describing the
> thread safety properties of their interpreter:
> http://kenai.com/projects/jruby/pages/RedBridge
The jython book covers concurrency extensively.
http://jythonpodcast.hostjava.net/jythonbook/en/1.0/Concurrency.html
HTH,
Alan.

Alan,
That is great - exactly what I was looking for!
w.r.t. modjy - I may switch over to using it; when i started my project it
wasn't clear to me that modjy could meet all my requirements (I had specific
concerns around module reloading) but after looking through the source I
think it will.
Thanks for your help,
--matt
Date: Thu, 1 Jul 2010 20:20:23 +0100
From: Alan Kennedy <jython-dev@...>
Subject: Re: [Jython-dev] Is jython thread safe?
To: jython-dev@...
Message-ID:
<AANLkTikQz26GnW2dp5LoQA7qRiv2N_XHd5wgJKcNq5-g@...>
Content-Type: text/plain; charset=ISO-8859-1
[Matt]
> Can anyone tell me if the PythonInterpreter is thread safe?
PythonInterpreter is thread-safe.
> That is, can two
> thread simultaneously call into the interpreter and evaluate different
> scripts? Are there any operations that are not thread safe and need to be
> synchronized by the caller?
But the exec and execfile methods of PythonInterpreter are not
thread-safe, so you need to synchronize them when
executing/evaluating.
> I have been searching all through the jython docs and googling like mad,
but
> I can't seem to find an authoritative answer to this question. Maybe if
> someone on this mailing list knows, he or she can create a short article
in
> the Jython wiki addressing this question.
That's a good idea. Probably a link to the relevant chapter of the
jython book would be a good idea.
> I am embedding Jython into a servlet container (tomcat) and am trying to
> figure out whether I can use a single interpreter for all requests
Yes you can use the same interpreter for all requests: see modjy for
an example of this.
[As an aside, is there a reason why you don't want to use modjy?]
> or I need
> to (a) put a big fat lock around the interpreter or (b) create an
> interpreter per request.
No, you don't have to do any do those things.
> As a point of reference, JRuby does an excellent job of describing the
> thread safety properties of their interpreter:
> http://kenai.com/projects/jruby/pages/RedBridge
The jython book covers concurrency extensively.
http://jythonpodcast.hostjava.net/jythonbook/en/1.0/Concurrency.html
HTH,
Alan.