python asyncio call_soon_threadsafe真的是线程安全的吗?

时间:2022-03-06 00:58:02

I have saw some code that use asyncio as a asynchronous task queue. Maybe like following

我看到一些使用asyncio作为异步任务队列的代码。也许喜欢以下

async def _send_email(address):
    pass

def send_email(address):
    task = asyncio.tasks.ensure_future(_send_email(address))
    task.add_done_callback(callback)

def init_worker(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

@app.route("/notify")
def do_jobs():
    # some code
    loop.call_soon_threadsafe(send_email, address)

loop = asyncio.new_event_loop()
worker = threading.Thread(target=init_worker, args=(loop,))
worker.setDaemon(True)
worker.start()

app.run()

I read the implementation of call_soon_threadsafe. It will append the task to loop._ready, the code here.

我读了call_soon_threadsafe的实现。它会将任务追加到loop._ready,这里是代码。

self._ready.append(handle)

But when the sub thread is executing _run_once, and pop the task from loop._ready, the code here.

但是当子线程正在执行_run_once时,从loop._ready中弹出任务,这里的代码。

handle = self._ready.popleft()

I'm not sure if the race condition exists or not. If it does not exist, under what circumstances should use the queue.Queue?

我不确定竞争条件是否存在。如果它不存在,在什么情况下应该使用queue.Queue?

Forgive my poor English.

原谅我可怜的英语。

1 个解决方案

#1


3  

As per https://bugs.python.org/issue15329#msg199368:

根据https://bugs.python.org/issue15329#msg199368:

The deque's append(), appendleft(), pop(), popleft(), and len(d) operations are thread-safe in CPython.

deque的append(),appendleft(),pop(),popleft()和len(d)操作在CPython中是线程安全的。

And there's info about Queue in the same message:

在同一条消息中有关于Queue的信息:

So, is deque a faster replacement for Queue.Queue or not?

那么,deque是否可以更快地替换Queue.Queue?

Yes, it is faster. The Queue module itself uses the deque internally. And the Queue is slowed down a bit through locks, function indirection, and additional features such as maxsize, join, and task_done.

是的,它更快。 Queue模块本身在内部使用deque。并且通过锁定,函数间接以及maxsize,join和task_done等附加功能,Queue会稍微减慢速度。

deque is just a datastructure, but Queue (and asyncio.Queue as well) provide much more, allowing for more flexible control flow.

deque只是一个数据结构,但Queue(以及asyncio.Queue)提供了更多,允许更灵活的控制流。

#1


3  

As per https://bugs.python.org/issue15329#msg199368:

根据https://bugs.python.org/issue15329#msg199368:

The deque's append(), appendleft(), pop(), popleft(), and len(d) operations are thread-safe in CPython.

deque的append(),appendleft(),pop(),popleft()和len(d)操作在CPython中是线程安全的。

And there's info about Queue in the same message:

在同一条消息中有关于Queue的信息:

So, is deque a faster replacement for Queue.Queue or not?

那么,deque是否可以更快地替换Queue.Queue?

Yes, it is faster. The Queue module itself uses the deque internally. And the Queue is slowed down a bit through locks, function indirection, and additional features such as maxsize, join, and task_done.

是的,它更快。 Queue模块本身在内部使用deque。并且通过锁定,函数间接以及maxsize,join和task_done等附加功能,Queue会稍微减慢速度。

deque is just a datastructure, but Queue (and asyncio.Queue as well) provide much more, allowing for more flexible control flow.

deque只是一个数据结构,但Queue(以及asyncio.Queue)提供了更多,允许更灵活的控制流。