日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/9/30 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python多线程资源共享_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個位置相關。我沒有使用口述,因為名單是有序的。在

我所做的是:

^{pr2}$

所以,用這個例子,我想可以把一個函數變成這樣:#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

總結

以上是生活随笔為你收集整理的python多线程资源共享_Python:关于多处理/多线程和共享资源的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。