threads and return values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Timothy Smith

    #1

    threads and return values

    say i'm issuing

    t = Thread(target=l ambda: WeeklyReportPDF .MakeReport(sel f.UserNameValue ,
    self.PassWordVa lue,ReportType, self.db))
    t.start()

    which works just fine, BUT how do i get access to the return value of
    WeeklyReportPDF .MakeReport() ??

  • Paul Rubin

    #2
    Re: threads and return values

    Timothy Smith <timothy@open-networks.netwri tes:
    t = Thread(target=l ambda:
    WeeklyReportPDF .MakeReport(sel f.UserNameValue ,
    self.PassWordVa lue,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(WeeklyRep ortPDF.MakeRepo rt(...)))
    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.

    Comment

    • Paul Rubin

      #3
      Re: threads and return values

      Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
      import Queue
      q = Queue()
      Oops, meant

      q = Queue.Queue()

      Comment

      Working...