The loop starts a fixed number of processes through multiprocessing.
for (i, data) in enumerate(datas): exit_event = Event() exit_events.append(exit_event) thread = Process(target=func, args=(data,i,exit_event)) thread.start()
Processes perform the same task and they complete their work only with manual interruption. At the very beginning of a code we create Manager
lock = Lock() manager = Manager() DATA_CONTROL=manager.dict()
And in the func function at the beginning I try to check the existence of the key
lock.acquire() status = DATA_CONTROL.has_key(data) lock.release()
What I get here is such an error
Process Process-2: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(*self._args, **self._kwargs) File "script.py", line 311, in func status = DATA_CONTROL.has_key(data) File "<string>", line 2, in has_key File "/usr/lib/python2.7/multiprocessing/managers.py", line 755, in _callmethod self._connect() File "/usr/lib/python2.7/multiprocessing/managers.py", line 742, in _connect conn = self._Client(self._token.address, authkey=self._authkey) File "/usr/lib/python2.7/multiprocessing/connection.py", line 169, in Client c = SocketClient(address) File "/usr/lib/python2.7/multiprocessing/connection.py", line 304, in SocketClient s.connect(address) File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 2] No such file or directory
Question - How to avoid mistakes? In principle, you need to implement a variable in which you could write and from which you could read both the main process and new processes.