Python:关于多进程/多线程和共享资源的问题
这是我找到的最简单的多线程示例:
import multiprocessing
import subprocess
def calculate(value):
return value * 10
if __name__ == '__main__':
pool = multiprocessing.Pool(None)
tasks = range(10000)
results = []
r = pool.map_async(calculate, tasks, callback=results.append)
r.wait() # Wait on the results
print results
我有两个列表和一个索引,用来访问每个列表中的元素。第一个列表的第i个位置和第二个列表的第i个位置是相关的。我没有使用字典,因为这些列表是有顺序的。
我之前做的事情大概是这样的:
for i in xrange(len(first_list)):
# do something with first_list[i] and second_list[i]
所以,基于这个例子,我想可以做一个这样的函数:
#global variables first_list, second_list, i
first_list, second_list, i = None, None, 0
#initialize the lists
...
#have a function to do what the loop did and inside it increment i
def function:
#do stuff
i += 1
但是,这样的话,i
就成了一个共享资源,我不确定这样是否安全。看起来我的设计也不太适合这种多线程的方法,但我不知道该怎么改进。
这是我想要的一个可运行的示例(编辑你想使用的图片):
import multiprocessing
import subprocess, shlex
links = ['http://www.example.com/image.jpg']*10 # don't use this URL
names = [str(i) + '.jpg' for i in range(10)]
def download(i):
command = 'wget -O ' + names[i] + ' ' + links[i]
print command
args = shlex.split(command)
return subprocess.call(args, shell=False)
if __name__ == '__main__':
pool = multiprocessing.Pool(None)
tasks = range(10)
r = pool.map_async(download, tasks)
r.wait() # Wait on the results
1 个回答
1
首先,创建一个包含元组的列表可能会很有帮助,比如说:
new_list[i] = (first_list[i], second_list[i])
这样,当你改变 i
的时候,就能确保你始终在处理来自 first_list
和 second_list
的相同项目。
其次,假设你的列表中 i
和 i-1
之间没有关系,你可以用你的函数来处理某个特定的 i
值,并为每个 i
值创建一个线程来处理。可以考虑:
indices = range(len(new_list))
results = []
r = pool.map_async(your_function, indices, callback=results.append)
r.wait() # Wait on the results
这样应该能满足你的需求。