[Twisted-Python] callFromThread with return
Andrew Bennetts
andrew-twisted at puzzling.org
Mon Oct 6 20:39:00 MDT 2003
On Mon, Oct 06, 2003 at 10:30:06PM -0400, Itamar Shtull-Trauring wrote:
>
> t.i,threads should indeed have a utility function that does this for
> you. It would need to turn Failures into exceptions and deal
> transparently with Deferreds, of course.
I posted something like this to this thread earlier:
-----
import threading, sys
from twisted.internet import reactor
def blockingCallFromThread(func):
e = threading.Event()
l = []
def wrapped_func():
try:
l.append(func)
except:
l.append(sys.exc_info())
l.append(0)
else:
l.append(1)
e.set()
reactor.callFromThread(wrapped_func)
e.wait()
result, ok = l
if ok:
return result
else:
# Whee! Cross-thread exceptions!
raise result[0], result[1], result[2]
----
It doesn't deal with Failures or Deferreds, but it does everything else.
Oh, and it's not tested. If I had already written test cases, I probably
would've checked it in by now ;)
(And I'm sure there's a better name for it than 'blockingCallFromThread'...)
-Andrew.
More information about the Twisted-Python
mailing list