from twisted.internet import defer, reactor
from twisted.internet import threads
import time
import sys
def execute_query(param):
def _query_result_callback(res):
sys.stderr.write('query result callback. res - %s\n' % str(res))
return res
sys.stderr.write('executing query, param - %s\n' % (param))
d = defer.succeed(param)
d.addCallback(_query_result_callback)
return d
def work_with_filesystem(param):
raise Exception(param)
sys.stderr.write('work with fyle system, param - %s\n' % param)
time.sleep(1.0)
return param
def callback(param):
sys.stderr.write('callback, param - %s\n' % param)
def errback(f):
sys.stderr.write('errback, param - %s\n' % (str(f.value)))
d1 = execute_query('d1')
d1.addCallback(callback)
d1.addErrback(errback)
d2 = threads.deferToThread(work_with_filesystem, 'd2')
d2.addCallback(callback)
d2.addErrback(errback)
reactor.callLater(5, reactor.stop)
reactor.run()
Так работает, но функции вызываются последовательно
from twisted.internet import defer, reactor
from twisted.internet import threads
import time
import sys
def execute_query(param):
def _query_result_callback(res):
sys.stderr.write('query result callback. res - %s\n' % str(res))
return res
sys.stderr.write('executing query, param - %s\n' % (param))
d = defer.succeed(param)
d.addCallback(_query_result_callback)
return d
def work_with_filesystem(param):
sys.stderr.write('work with fyle system, param - %s\n' % param)
time.sleep(1.0)
return param
def callback(param):
sys.stderr.write('callback, param - %s\n' % param)
def errback(f):
sys.stderr.write('errback, param - %s\n' % (str(f.value)))
d = threads.deferToThread(work_with_filesystem, 'qwe')
d.addCallback(execute_query)
d.addCallback(callback)
d.addErrback(errback)
reactor.callLater(5, reactor.stop)
reactor.run()