【问题标题】:Using multiprocessing module with BaseHTTPRequestHandler将多处理模块与 BaseHTTPRequestHandler 一起使用
【发布时间】:2021-06-10 07:33:57
【问题描述】:

我正在尝试使用 python 多处理模块通过 http.server.BaseHTTPRequestHandler 模块在另一个线程中运行服务器。我被卡住了,遇到了“_thread.lock”问题。

我不想使用线程模块,因为我宁愿在多处理模块中使用真正的多线程。

如果有人知道我做错了什么,或者可以为我指出一个好的库来使用,那就太棒了。

import multiprocessing
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler

if __name__ == '__main__':
  httpd = ThreadingHTTPServer(('localhost', 4433), BaseHTTPRequestHandler)

  manager = multiprocessing.Manager()
  manager.http_server = httpd
  running_server = multiprocessing.Process(target=manager.http_server.serve_forever)
  running_server.start()

堆栈跟踪:

File "/Users/redacted/python/test2/test1.py", line 10, in <module>
    running_server.start()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.lock' object

【问题讨论】:

    标签: python python-multiprocessing python-multithreading httpserver


    【解决方案1】:

    在使用多进程模块时,Python 使用 pickle 将对象传递给另一个进程。在您的情况下,httpserver 中使用的线程锁是不可腌制的。所以它报告错误。 你可以做的是在另一个进程中完全像这样启动 http 服务器:

    import multiprocessing
    from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
    
    def startServer():
        httpd = ThreadingHTTPServer(('localhost', 4433), BaseHTTPRequestHandler)
        httpd.serve_forever()
        
    if __name__ == '__main__':
      manager = multiprocessing.Manager()
      running_server = multiprocessing.Process(target=startServer)
      running_server.start()
    

    另外,您可能想尝试使用 4433 以外的其他端口。我无法连接到我的 Windows 机器上的此端口。但如果我使用 8000 一切正常。

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多