不能在HTTPServer上使用多处理队列来腌制_thread.lock对象,但可以在自定义类上

时间:2019-08-22 09:08:40

标签: python-3.x multiprocessing queue pickle python-multiprocessing

  • 我正在为我创建的RPA计算机编写一个多处理中心。

    • 有2个“模块”需要通过2个队列进行通信。
    • 我可以生成一个模块的子进程/多进程,而不能生成另一个模块。
  • 我已经尝试过:

    • __init__内设置队列
    • 在生成之前通过队列
    • 在生成后通过队列

from http.server import HTTPServer
from http.server import BaseHTTPRequestHandler

import multiprocessing as mp


## GLOBAL:
procs = []


class Zombie(object):
  """ This one spawns just fine """
  def __init__(self, jQ, mQ):
    self.mQ = mQ
    self.jQ = jQ

  def farm(self):
    print('I am working ... um .. brains?')


class MyRequestHandler(BaseHTTPRequestHandler):
  """ It is what it is """

class Server(HTTPServer):
  """ Handle requests in a single thread """
  def __init__(self, **kwargs):
    self.jQ = kwargs.pop('jQ')
    self.mQ = kwargs.pop('mQ')
    self.socket = kwargs.pop('socket')
    self.server = kwargs.pop('server')
    super(Server, self).__init__(self.socket, self.server)
    print('ServerClass INIT')


def spawn_zombie(jQ, mQ):
  farmer = Zombie(jQ, mQ)
  proc = mp.Process(target=farmer.farm)
  procs.append(proc)


def spawn_server(jQ, mQ):
  rh = MyRequestHandler
  srv = Server(socket=('0.0.0.0', 9998), server=rh, jQ=jQ, mQ=mQ)
  proc = mp.Process(target=srv.serve_forever)
  procs.append(proc)


if __name__=="__main__":
  # use spawn method to avoid bugs
  mp.set_start_method('spawn')

  manager = mp.Manager()
  jQ = manager.Queue()
  mQ = manager.Queue()

  # start all the things ...
  zmb_farm = spawn_zombie(jQ, mQ)
  http_srv = spawn_server(jQ, mQ)

  for proc in procs:
    proc.daemon = False
    proc.start()

  print("All's started what's getting started ... ")

  for proc in procs:
    proc.join()

预期结果:两个模块都应产生,并且服务器应服务。 我得到的...是这样:

ServerClass INIT
Traceback (most recent call last):
  File "classtest.py", line 62, in <module>
    proc.start()
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/process.py", line 105, in start
    self._popen = self._Popen(self)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/popen_fork.py", line 20, in __init__
    self._launch(process_obj)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects
I am working ... um .. brains?

0 个答案:

没有答案