threads and return values
Paul Rubin
http
Wed Sep 13 21:48:18 EDT 2006
Timothy Smith <timothy at open-networks.net> writes:
> t = Thread(target=lambda:
> WeeklyReportPDF.MakeReport(self.UserNameValue,
> self.PassWordValue,ReportType,self.db))
> t.start()
>
> which works just fine, BUT how do i get access to the return value of
> WeeklyReportPDF.MakeReport() ??
You can't. Make the function send you a message through some
synchronized communication mechanism. The Pythonic favorite way to do
that is with Queue, even when it's just one value (untested):
import Queue
q = Queue()
t = Thread(target = lambda q=q: q.put(WeeklyReportPDF.MakeReport(...)))
t.start()
...
Now if you say
value = q.get()
the caller will block until WeeklyReportPDF.MakeReport returns. If
you say
value = q.get(False)
the False argument says not to block, so if WeeklyReportPDF.MakeReport
hasn't yet returned, q.get will raise the Queue.Empty exception, which
you can then catch and deal with. Another arg lets you specify a
timeout:
value = q.get(False, 3.0)
blocks for up to 3 seconds, then raises Queue.Empty.
More information about the Python-list
mailing list