[Twisted-Python] Suggested API addition to threadtask
Itamar Shtull-Trauring
lists at itamarst.org
Sun Sep 16 04:23:53 MDT 2001
Hi all,
Background:
internet.threadtask lets threads schedule tasks that are then executed by
the main event loop (Active Object pattern).
Problem:
The results of these tasks are not returned to the threads, nor do the
threads have any way of seeing if any exceptions were raised when doing the
task. In most cases this is not an issue (e.g. writing data to a connection)
but it is a problem with things like opening a new tcp.Client connection.
Suggested Solution:
Optionally allow returning a DeferredResult object when scheduling tasks,
which can then be used to get the result (or exception) of the task when it
is eventually executed by the event loop thread.
Example:
Notice how we can catch the exceptions raised by the scheduled task.
==================================================
def toInt(x):
return int(x)
dresult = threadtask.scheduleWithResult(toInt, args=("23",))
if dresult.haveResult():
try:
result = dresult.get()
except ValueError:
print "Not an integer"
else:
print "It's an integer: %d" % result
===================================================
Feedback I'm looking for:
1) Initially I had the operation that gets the result (DeferredResult.get)
block, but this is a bad idea since it can deadlock threads waiting for a
result that will never be returned, e.g. when shutting down the server.
Instead now I raise a NotReady exception if no result is available. Would
callbacks be better?
2) Better method names, "scheduleWithResult" is ugly.
3) Is this idea (DeferredResults) applicable to internet.task as well?
Attached is my updated code.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: threadtask.py
URL: </pipermail/twisted-python/attachments/20010916/6470e294/attachment.ksh>
More information about the Twisted-Python
mailing list