如何使此功能无阻塞?

时间:2019-04-09 16:36:32

标签: python python-3.x asynchronous

我需要使函数s_settings不停止编程,我在异步函数中调用此函数,在set_settings中,我从服务器获取数据,在send_settings中,我将数据发送到服务器。我无法使方法(set_settings和send_settings)异步。但是我需要使此功能不阻塞


def s_settings(id, _user_settings, all_settings):
    if 'category' in set_settings(str(id)):
        _user_settings[str(id)] = set_settings(str(id)) # Get from the Server
    print(user_settings)    
    _user_settings[str(id)]['category'].append(
        all_settings[str(id)]['category'])
    _user_settings[str(id)]['time_to_receive'].append(
        all_settings[str(id)]['time_to_receive'])
    _user_settings[str(id)]['language'].append(all_settings[str(id)]['language'])
    send_settings(_user_settings[str(id)], str(id)) # Send to the server

2 个答案:

答案 0 :(得分:0)

那真是太可惜了。您必须将此函数建模为协程。但是,由于您不能依靠事件循环为您轮询对send_settings的阻塞函数调用,因此可以生成一个单独的线程,在该线程中执行该函数。您的协程应该是非阻塞的,可以轮询该线程并获得结果。这可能是一个相当复杂的实现。

答案 1 :(得分:0)

已由loop.run_in_executor()

解决