[英]Python thread safe asyncio
I am trying to run my sync functions using asyncio.run_in_executor我正在尝试使用 asyncio.run_in_executor 运行我的同步功能
My coroutines shares data and update data.我的协程共享数据和更新数据。 The run in executor starts threads.
run in executor 启动线程。 Do I need to make my objects thread and asyncio safe?
我需要让我的对象线程和异步安全吗?
import asyncio
from concurrent.futures import ThreadPoolExecutor
class shared:
#complex object
def func(a, b):
# blocking calls
return a + b
async def main(loop):
result = await loop.run_in_executor(None, func, "Hello,", " world!")
UpdateSharedObject(result)
Start main as 5 tasks using create_task
If you are accessing (and especially making changes) to the same resource from different threads.如果您正在从不同的线程访问(尤其是进行更改)相同的资源。 Then I highly recommend using synchronization primitives .
然后我强烈推荐使用 同步原语。 Because Python does not provide this thread-safety in all cases.
因为 Python 并非在所有情况下都提供这种线程安全。 Many Python operations are atomic, but not all.
许多 Python 操作是原子的,但不是全部。 Actually, single opcodes are thread-safe.
实际上,单个操作码是线程安全的。
Examples of thread-safe operations:线程安全操作的示例:
L.append(x)
L1.extend(L2)
x = L[i]
x = L.pop()
These are not:这些不是:
i = i+1
L.append(L[-1])
L[i] = L[j]
D[x] = D[x] + 1
More info here: https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe更多信息: https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
As for asyncio
, I agree with the @user4815162342 in the comments and advise you to redesign the program so that the functions executed in the threads are pure functions and do not change shared objects.至于
asyncio
,我同意评论中的@user4815162342 并建议您重新设计程序,以便在线程中执行的函数是纯函数,并且不会更改共享对象。 Because the use of thread synchronization, together with cooperative multitasking ( asyncio
), can greatly complicate the code and lead to subtle bugs.因为使用线程同步以及协作多任务(
asyncio
)会使代码变得非常复杂并导致细微的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.