混合使用Deferred和deferToThread在单独的线程中运行阻塞代码

2024-04-20 05:42:10 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的pythontwisted应用程序中,我需要从客户机接收数据,执行一些数据库操作,并根据数据在不同的线程中运行一些块代码。在

到目前为止,我已经:

d = get_user(user_id)

d.addCallback(do_something_with_input_data, input_data)
d.addCallback(run_blocking_code)
d.addCallback(save_data_into_db)
d.addCallback(response_to_client)

@defer.inlineCallbacks
def get_user(self, user_id):
    user = yield get_user_from_db(user_id)
    defer.returnValue(user)

def do_something_with_input_data(user, input_data):
    # do smth...
    return results

@defer.inlineCallbacks
def run_blocking_code(results)

    threads.deferToThread(run_in_separate_thread, results)
    return results

@defer.inlineCallbacks
def save_data_into_db(results)
    yield save_in_db(results)
    def.returnValue('OK')

def response_to_client(response)
    # send 'OK' to client

这是调用run_blocking_code()中的deferToThread()的好方法吗?如果是这样,如何使save_data_into_db()等待线程结束?在


Tags: runidinputdbdatagetsavedef
1条回答
网友
1楼 · 发布于 2024-04-20 05:42:10

我认为总体概念是好的。我将添加一些errbacks,您还需要调整run_blocking_代码函数:

from twisted.internet import defer, threads

@defer.inlineCallbacks
def run_blocking_code(results):

    # the `deferToThread` returns a deferred
    d = threads.deferToThread(run_in_separate_thread, results)

    # let's wait for it here (need a yield for functions decorated with `inlineCallbacks`)
    yield d

    # now return its value to the next function in the callback chain
    defer.returnValue(d.result)

相关问题 更多 >