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
Here's the simplest multi threading example I found so far:
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 have two lists and one index to access the elements in each list. The ith position on the first list is related to the ith position on the second. I didn't use a dict because the lists are ordered.
What I was doing was something like:
for i in xrange(len(first_list)):
# do something with first_list[i] and second_list[i]
So, using that example, I think can make a function sort of like this:
#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
But, that makes i
a shared resource and I'm not sure if that'd be safe. It also seems to me my design is not lending itself well to this multithreaded approach, but I'm not sure how to fix it.
Here's a working example of what I wanted (Edit an image you want to use):
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,创建一个元组列表可能会有所帮助,例如
这样,当您更改
i
时,您可以确保始终对first_list
中的相同项目进行操作> 和second_list
。其次,假设列表中的
i
和i-1
条目之间没有关系,您可以使用函数对给定的 i 值进行操作,并生成一个线程处理每个 i 值。考虑一下这应该会给你你想要的。
First off, it might be beneficial to make one list of tuples, for example
That way, as you change
i
, you ensure that you are always operating on the same items fromfirst_list
andsecond_list
.Secondly, assuming there are no relations between the
i
andi-1
entries in your lists, you can use your function to operate on one given i value, and spawn a thread to handle each i value. ConsiderThis should give you what you want.