我正在嘗試構建一個運行最終利用多處理庫的函式的 Flask 應用程式。
import time
from flask import Flask, request
import multiprocessing as mp
app = Flask(__name__)
def proc():
time.sleep(10)
@app.route('/greeting')
def greeting():
p = mp.Process(target=proc, args=())
p.start()
return ('Hello there')
app.run(port=8000)
當我去http://localhost:8000/greeting嘗試呼叫時出現以下錯誤,就會出現問題p.start()。
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
prepare(preparation_data)
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/michaelequi/Github/drlm/apps/mp_flask.py", line 18, in <module>
app.run(port=8000)
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/flask/app.py", line 920, in run
run_simple(t.cast(str, host), port, self, **options)
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 1010, in run_simple
inner()
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 950, in inner
srv = make_server(
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 782, in make_server
return ThreadedWSGIServer(
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 688, in __init__
super().__init__(server_address, handler) # type: ignore
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 48] Address already in use
如果我將代碼更改為 debug=True ( app.run(port=8000, debug=True)) 我會得到一個不同的錯誤!
Traceback (most recent call last):
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 950, in inner
srv = make_server(
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 782, in make_server
return ThreadedWSGIServer(
File "/Users/michaelequi/Github/drlm/venv/drlm/lib/python3.9/site-packages/werkzeug/serving.py", line 676, in __init__
real_sock = socket.fromfd(fd, self.address_family, socket.SOCK_STREAM)
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 545, in fromfd
return socket(family, type, proto, nfd)
File "/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 232, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [Errno 38] Socket operation on non-socket
無論我在哪個埠上運行燒瓶服務器,都會發生這種情況。任何人都知道這可能是什么原因以及如何解決它?我在 MacOS Big Sur 上使用 Python 3.9.9。
uj5u.com熱心網友回復:
代替
app.run(port=8000)
和
if __name__ == '__main__':
app.run(port=8000)
使用多處理時,任何“頂級”的東西都會在每個執行緒中運行。所以你的主執行緒和你正在創建的執行緒都嘗試打開埠 8000
uj5u.com熱心網友回復:
錯誤告訴我們:
OSError: [Errno 48] Address already in use
這可能是因為其他東西正在使用同一個埠,甚至是同一個程式的一個實體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/412094.html
標籤:
