Python多处理锁 [英] Python Multiprocessing Locks

查看:110
本文介绍了Python多处理锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此多处理代码按预期工作.它创建了4个Python进程,并使用它们来打印数字0到39,每次打印后都有延迟.

This multiprocessing code works as expected. It creates 4 Python processes, and uses them to print the numbers 0 through 39, with a delay after each print.

import multiprocessing
import time

def job(num):
  print num
  time.sleep(1)

pool = multiprocessing.Pool(4)

lst = range(40)
for i in lst:
  pool.apply_async(job, [i])

pool.close()
pool.join()

但是,当我尝试使用multiprocessing.Lock来防止将多个进程打印到标准输出时,程序只是立即退出而没有任何输出.

However, when I try to use a multiprocessing.Lock to prevent multiple processes from printing to standard out, the program just exits immediately without any output.

import multiprocessing
import time

def job(lock, num):
  lock.acquire()
  print num
  lock.release()
  time.sleep(1)

pool = multiprocessing.Pool(4)
l = multiprocessing.Lock()

lst = range(40)
for i in lst:
  pool.apply_async(job, [l, i])

pool.close()
pool.join()

为什么引入multiprocessing.Lock会使此代码不起作用?

Why does the introduction of a multiprocessing.Lock make this code not work?

更新:它在全局声明锁时起作用(我做了一些非确定性测试以检查锁是否正常工作),与上面的代码相反,该代码将锁作为参数传递(Python的多处理文档显示了锁)作为参数传递).下面的代码具有全局声明的锁,而不是在上面的代码中作为参数传递.

Update: It works when the lock is declared globally (where I did a few non-definitive tests to check that the lock works), as opposed to the code above which passes the lock as an argument (Python's multiprocessing documentation shows locks being passed as arguments). The code below has a lock declared globally, as opposed to passing as an argument in the code above.

import multiprocessing
import time

l = multiprocessing.Lock()

def job(num):
  l.acquire()
  print num
  l.release()
  time.sleep(1)

pool = multiprocessing.Pool(4)

lst = range(40)
for i in lst:
  pool.apply_async(job, [i])

pool.close()
pool.join()

推荐答案

如果将pool.apply_async更改为pool.apply,则会出现以下异常:

If you change pool.apply_async to pool.apply, you get this exception:

Traceback (most recent call last):
  File "p.py", line 15, in <module>
    pool.apply(job, [l, i])
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 244, in apply
    return self.apply_async(func, args, kwds).get()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
    raise self._value
RuntimeError: Lock objects should only be shared between processes through inheritance

pool.apply_async只是将其隐藏.我讨厌这样说,但是使用全局变量可能是您的示例的最简单方法.我们只是希望速激肽不会帮助您.

pool.apply_async is just hiding it. I hate to say this, but using a global variable is probably the simplest way for your example. Let's just hope the velociraptors don't get you.

这篇关于Python多处理锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆