![](/img/trans.png)
[英]Is python asyncio call_soon_threadsafe really thread-safe?
[英]Python thread safe asyncio
我正在尝试使用 asyncio.run_in_executor 运行我的同步功能
我的协程共享数据和更新数据。 run in executor 启动线程。 我需要让我的对象线程和异步安全吗?
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
如果您正在从不同的线程访问(尤其是进行更改)相同的资源。 然后我强烈推荐使用 同步原语。 因为 Python 并非在所有情况下都提供这种线程安全。 许多 Python 操作是原子的,但不是全部。 实际上,单个操作码是线程安全的。
线程安全操作的示例:
L.append(x)
L1.extend(L2)
x = L[i]
x = L.pop()
这些不是:
i = i+1
L.append(L[-1])
L[i] = L[j]
D[x] = D[x] + 1
更多信息: https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
至于asyncio
,我同意评论中的@user4815162342 并建议您重新设计程序,以便在线程中执行的函数是纯函数,并且不会更改共享对象。 因为使用线程同步以及协作多任务( asyncio
)会使代码变得非常复杂并导致细微的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.