Python:关于多处理/多线程和共享资源的问题

发布于 2024-12-05 01:08:14 字数 1504 浏览 1 评论 0原文

这是迄今为止我发现的最简单的多线程示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-12-12 01:08:14

首先,创建一个元组列表可能会有所帮助,例如

new_list[i] = (first_list[i], second_list[i])

这样,当您更改 i 时,您可以确保始终对 first_list 中的相同项目进行操作> 和 second_list

其次,假设列表中的 ii-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

这应该会给你你想要的。

First off, it might be beneficial to make one list of tuples, for example

new_list[i] = (first_list[i], second_list[i])

That way, as you change i, you ensure that you are always operating on the same items from first_list and second_list.

Secondly, assuming there are no relations between the i and i-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. Consider

indices = range(len(new_list))
results = []
r = pool.map_async(your_function, indices, callback=results.append)
r.wait() # Wait on the results

This should give you what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文