小编Yin*_*nLu的帖子

python ThreadPoolExecutor 线程安全吗?

就像期货写同一个文件时我应该手动加锁以保证他们一一写吗?

  • 我的意思是concurrent.futures.ThreadPoolExecutor

  • 而且我知道 java executor 是线程安全的

一个例子:

def task():
    with open("somefile", "a") as fh:
        fh.write(part_of_data)
    do_something()
    with open("somefile", "a") as fh:
        fh.write(other_data)
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我想确保每个任务都other_data附加part_of_data在 ThreadPoorExecuter 中执行任务的时间旁边

我不确定该with语句是原子操作,但如果不是,则执行程序还应保证文件已正确打开和关闭

python concurrency multithreading

5
推荐指数
1
解决办法
2967
查看次数

标签 统计

concurrency ×1

multithreading ×1

python ×1