在进程之间共享变量 [英] Sharing a variable between processes
问题描述
我有一个下载功能,可并行下载多个文件。
我使用 multiprocessing.Pool.map_async
为了下载不同的同一个文件的块。
我想显示一个状态栏的下载。为此,我需要知道已经下载的总字节数( total_bytes_dl
)。
I have a downloader function that downloads multiple files parallely.
I use multiprocessing.Pool.map_async
in order to download different chunks of the same file.
I would like to show a statusbar of the download. For this, I need to know the total bytes that has been already downloaded (total_bytes_dl
).
pool = multiprocessing.Pool(processes)
mapObj = pool.map_async(f, args)
while not mapObj.ready():
status = r"%.2f MB / %.2f MB" % (total_bytes_dl / 1024.0 / 1024.0, filesize / 1024.0 / 1024.0,)
status = status + chr(8)*(len(status)+1)
print status,
time.sleep(0.5)
有没有办法设置一个变量将在所有这些进程和主进程之间共享,所以每个进程都可以追加刚刚下载的字节数?
Is there a way to set a variable that will be shared among all these processes AND the main process, so every process can append the amount of bytes that has just downloaded?
推荐答案
解决方案是帮助新进程,并传递共享ctypes值:
The solution was to intilize the new process and pass the shared ctypes value:
from ctypes import c_int
import dummy
shared_bytes_var = multiprocessing.Value(c_int)
def Func(...):
....
pool = multiprocessing.Pool(initializer=_initProcess,initargs=(shared_bytes_var,))
....
def _initProcess(x):
dummy.shared_bytes_var = x
这篇关于在进程之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!