以下两个程序在功能上是等效的(在Ubuntu 16.04上使用python3.5进行了测试)。
为什么对于Manager()返回的信号量而不是全局变量,为什么更喜欢使用代理对象?
全局变量:
import multiprocessing as mp
import random
import time
SEMAPHORE = mp.BoundedSemaphore(4)
def main():
pool = mp.Pool(4)
for i in range(10):
SEMAPHORE.acquire()
pool.apply_async(task, args=(i,), error_callback=log_error)
pool.close()
pool.join()
def task(n):
print('Start: %d' % n)
time.sleep(0.2 + 0.2 * random.random())
SEMAPHORE.release()
print('End: %d' % n)
def log_error(exception):
print('Got error: %s' % exception)
if __name__ == '__main__':
main()
Manager()中的代理对象
import multiprocessing as mp
import random
import time
def main():
with mp.Manager() as manager:
semaphore = manager.BoundedSemaphore(4)
pool = mp.Pool(4)
for i in range(10):
semaphore.acquire()
pool.apply_async(task, args=(semaphore, i), error_callback=log_error)
pool.close()
pool.join()
def task(sem, n):
print('_Start: %d' % n)
time.sleep(0.2 + 0.2 * random.random())
sem.release()
print('_End: %d' % n)
def log_error(exception):
print('Got error: %s' % exception)
if __name__ == '__main__':
main()