我想在多處理(https://docs.python.org/2/library/multiprocessing.html)創建的線程之間共享一個3 gb的bitarray(https://pypi.python.org/pypi/bitarray/0.8.1)。python多處理:共享比特數組(bitarray 0.8.1)
我只想讀取bitarray而不修改它。下面的python 2.7代碼真的可以嗎?不知何故,它似乎沒有使用ctypes(docs.python.org/2/library/ctypes.html)。
import multiprocessing as mp
import bitarray
import time
def f(x):
n = 0
#print shared_arr[n:(10+n)] & shared_arr[n:(10+n)]
print "worker %d started at time %s" % (x, str(time.time()-start_time))
print "running %d. bit %d of shared array is: " % (x, x) +str(shared_arr[n:(10+n)])
time.sleep(2)
print "ending %d at time %s" %(x, str(time.time()-start_time))
return x*x
def main():
print "The number of cpu is %d" % (mp.cpu_count())
num_cpu_core = mp.cpu_count()
n = 0
global shared_arr
global start_time
start_time = time.time()
shared_arr = bitarray.bitarray(18)
shared_arr[:] = 0
shared_arr[(n+5):(n+7)] = 1
a = 10
pool = mp.Pool(processes = num_cpu_core) # not saving one for the master process
pool.map_async(f, range(10))
pool.close()
pool.join()
main()
多處理不創建線程,它創建*進程*。 –