Python3 многопроцессорный общий словарь потребляет весь процесс - PullRequest
1 голос
/ 24 февраля 2020

Я новичок в многопроцессорности, я хотел бы использовать многопроцессорность для параллельного кода, работающего с потоковыми данными. Чтобы начать хорошо, я кодировал ниже и получил ошибку. Не могли бы вы сказать мне правильный способ печати на экране.

Код:

import sys
from multiprocessing import Process, Manager
import time

def producer(dic, name):
    for i in range(10000):
        dic["A"] = i
        time.sleep(2)


def consumer(dic, name):
    for i in range(10000):
        aval = dic.get("A")
        #print(f" {name} - Val = {aval}")
        sys.stdout.write(f" {name} - Val = {aval}")
        sys.stdout.flush()
        time.sleep(2.2)


if __name__ == '__main__':
    manager = Manager()
    dic = manager.dict()
    Process(target=producer, args=(dic,"TT")).start()
    time.sleep(1)
    Process(target=consumer, args=(dic,"Con1")).start()
    Process(target=consumer, args=(dic,"Con2")).start()

Когда я запускаю то же самое в консоли windows, у меня появляется ошибка ниже, как я могу Функция печати Consumer's print в консоли. Спасибо

(base) PS D:\> python .\mulpro.py
Process Process-3:
Process Process-4:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 811, in
_callmethod
    conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 297, in _
bootstrap
    self.run()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 99, in ru
n
    self._target(*self._args, **self._kwargs)
  File "D:\mulpro.py", line 19, in consumer
    aval = dic.get("A")
  File "<string>", line 2, in get
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 815, in
_callmethod
    self._connect()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 802, in
_connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 490, i
n Client
    c = PipeClient(address)
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 691, i
n PipeClient
    _winapi.WaitNamedPipe(address, 1000)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 811, in
_callmethod
    conn = self._tls.connection
FileNotFoundError: [WinError 2] The system cannot find the file specified
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 297, in _
bootstrap
    self.run()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 99, in ru
n
    self._target(*self._args, **self._kwargs)
  File "D:\mulpro.py", line 19, in consumer
    aval = dic.get("A")
  File "<string>", line 2, in get
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 815, in
_callmethod
    self._connect()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 802, in
_connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 490, i
n Client
    c = PipeClient(address)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 691, i
n PipeClient
    _winapi.WaitNamedPipe(address, 1000)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process Process-2:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 297, in _
bootstrap
    self.run()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 99, in ru
n
    self._target(*self._args, **self._kwargs)
  File "D:\mulpro.py", line 13, in producer
    dic["A"] = i
  File "<string>", line 2, in __setitem__
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\managers.py", line 818, in
_callmethod
    conn.send((self._id, methodname, args, kwds))
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 206, i
n send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 280, i
n _send_bytes
    ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
BrokenPipeError: [WinError 232] The pipe is being closed
...