简体   繁体   English

Python多处理过程中的错误

[英]Error in Python multiprocessing process

I am trying a write a python code having multiple processes whose structure and flow is something like this: 我正在尝试编写一个具有多个进程的python代码,这些进程的结构和流程如下所示:

import multiprocessing
import ctypes
import time
import errno
m=multiprocessing.Manager()
mylist=m.list()
var1=m.Value('i',0)
var2=m.Value('i',1)
var3=m.Value('i',2)
var4=m.Value(ctypes.c_char_p,"a")
var5=m.Value(ctypes.c_char_p,"b")
var6=3
var7=4
var8=5
var9=6
var10=7
def func(var1,var2,var4,var5,mylist):
    i=0
    try:
        if var1.value==0:
            print var2.value,var4.value,var5.value
            mylist.append(time.time())
        elif var1.value==1:
            i=i+2
            print var2.value+2,var4.value,var5.value
            mylist.append(time.time())
    except IOError as e:
        if e.errno==errno.EPIPE:
            var3.value=var3.value+1
            print "Error"
def work():
    for i in range(var3.value):
        print i,var6,var7,va8,var9,var10
        p=multiprocessing.Process(target=func,args=(var1,var2,var4,var5,mylist))
        p.start()
work()

When I run this code, sometimes it works perfectly, sometimes it does not run for exact amount of loop counts and sometimes I get following error: 当我运行此代码时,有时它可以完美运行,有时不能在确切的循环计数下运行,有时会出现以下错误:

0
1
Process Process-2:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap
    self.run()
  File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "dummy.py", line 19, in func
    if var1.value==0:
  File "/usr/lib64/python2.6/multiprocessing/managers.py", line 1005, in get
    return self._callmethod('get')
  File "/usr/lib64/python2.6/multiprocessing/managers.py", line 722, in _callmethod
    self._connect()
  File "/usr/lib64/python2.6/multiprocessing/managers.py", line 709, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "/usr/lib64/python2.6/multiprocessing/connection.py", line 149, in Client
    answer_challenge(c, authkey)
  File "/usr/lib64/python2.6/multiprocessing/connection.py", line 383, in answer_challenge
    message = connection.recv_bytes(256)         # reject large message
EOFError

What does this error mean? 这个错误是什么意思? What wrong am I doing here? 我在这里做什么错? What this error indicates? 此错误表示什么? Kindly guide me to the correct path. 请引导我走正确的道路。 I am using CentOS 6.5 我正在使用CentOS 6.5

Working with shared variables in multiprocessing is tricky. 在多处理中使用共享变量非常棘手。 Because of the python Global Interpreter Lock (GIL), multiprocessing is not directly possible in Python. 由于存在python全局解释器锁定(GIL),因此无法在Python中直接进行多处理。 When you use the multiprocessing module, you can launch several task on different process, BUT you can't share the memory. 使用multiprocessing模块时,可以在不同的进程上启动多个任务,但不能共享内存。 In you case, you need this so you try to use shared memory. 在这种情况下,您需要这样做,以便尝试使用共享内存。 But what happens here is that you have several processes trying to read the same memory at the same time. 但是这里发生的是,您有多个进程试图同时读取同一内存。 To avoid memory corruption, a process lock the memory address it is currently reading, forbidding other processes to access it until it finishes reading. 为避免内存损坏,一个进程将锁定其当前正在读取的内存地址,禁止其他进程对其进行访问,直到完成读取为止。 Here you have 3 processes trying to evaluate var1.value in the first if loop of your func : the first process read the value, and the other are blocked, raising an error. 在这里,您有3个进程试图在func的第一个if循环中评估var1.value :第一个进程读取该值,另一个进程被阻塞,从而引发错误。 To avoid this mechanism, you should always manage the Lock of your shared variables yourself. 为了避免这种机制,您应该始终自己管理共享变量的Lock You can try with syntax: 您可以尝试使用语法:

var1=multiprocessing.Value('i',0) # create shared variable
var1.acquire() # get the lock : it will wait until lock is available
var1.value # read the value
var1.release() # release the lock

External documentation : 外部文件:

Locks : https://docs.python.org/2/librar/multiprocessing.html#synchronization-between-processes GIL : https://docs.python.org/2/glossary.html#term-global-interpreter-lock 锁: https: //docs.python.org/2/librar/multiprocessing.html#synchronization-between-processes GIL: https ://docs.python.org/2/glossary.html#term-global-interpreter-lock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM