如果我使用线程运行这个程序,getUrl
中会有竞争条件吗?我正在多个线程中更改data['key']
的值。我需要将整个data
传递给我发出的请求,基本上,一组键是固定的,对于我发出的每个线程调用,只有名为key
的键会改变
import requests
from concurrent.futures import ThreadPoolExecutor
def getUrl(url, value):
data['key'] = value # will there be a race condition here
return requests.get(url, data=data).text # and when the value is passed here
data = {'key': 1, 'fixedKey': 'fixedValue', 'fixedKey2': 'fixedValue2'}
resultArray = []
threadPool = ThreadPoolExecutor(32)
for i in range(100):
resultArray.append(threadPool.submit(getUrl, 'https://google.com', i))
Thread Safety in Python's dictionary我检查了这个,但我的困惑是,当我在data['key'] = value
中进行设置时,线程是否会切换上下文,然后其他一些线程会进行更改,而下一行现在有一个不同线程设置的新值
范例
Value set by thread 1
data['key'] = 1
Context Switch
Value set by thread 2
data['key'] = 2
Context Switch back to old thread 1
is data['key'] = `2` now? I would necessarily want the value `1`
如果我使用锁,那么我将失去这里的并发性
存在竞态条件,因为
data
在线程之间共享,您可以从线程中对其进行变异可视化此竞争条件的一种简单方法是使用
sleep
和print
模拟request
调用:您将看到传递给
getUrl
的值有时与存储在data
中的值不同一种解决方法是在变异前将
data
复制到局部您当前的代码确实存在竞争条件,因为您正在不同的线程中更改并使用相同的全局变量。一种简单的方法是使用本地副本:
相关问题 更多 >
编程相关推荐