开发者

getting result from a function running "deferToThread"

I have recently started working on twisted not much familiar with its functions.I have a problem related to "deferToThread" method...my code is here to use this method

from twisted.internet.t开发者_如何学JAVAhreads import deferToThread
from twisted.internet import reactor

results=[]
class Tool(object):

    def exectool(self,tool):

#     print "Test Class Exec tool running..........."
      exec tool
      return

    def getResult(self,tool):
        return results.append(deferToThread(self.exectool, tool))

to=Tool()

to.getResult(tools)


f=open(temp).read()
obj_tool=compile(f, 'a_filename', 'exec')

[ at 0x8ce7020, file "a_filename", line 1>, at 0x8cd4e30, file "a_filename", line 2>]

I am passing tools one by one in getResults() method it executs successfully & prints the results what script written in the file objects. I have to store the result of tools executing in some variable so that I can save it in database.How to achieve this cause when i call re=to.getResult(tools) and print "re" it prints none. I HAVE TO STORE ITS RESULTS IN DATABASE? IS THERE SOMETHING I CAN DO?

thanx in advance


There are two problems here.

First, deferToThread will not work if you never start the reactor. Hopefully this code snippet was actually extracted from a larger Twisted-using application where the reactor is running, so that won't be an actual problem for you. But you shouldn't expect this snippet to work unless you add a reactor.run() call to it.

Second, deferToThread returns a Deferred. The Deferred fires with the result of the callable you passed in. This is covered in the API documentation. Many APIs in Twisted return a Deferred, so you might want to read the documentation covering them. Once you understand how they work and how to use them, lots of things should be quite a bit easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜