Python: 运行 eel 多线程导致错误

Python: running eel with multithreading results in a error

注意:-

  1. 如果运行鳗鱼不穿线,效果很好
  2. 如果我 运行 没有鳗鱼穿线,它工作得很好
  3. 如果我对 eel 使用 Multiprocessing,它工作得很好
  4. 我试过多次卸载重装python,我也试过多次卸载重装eel,但至今没有成功...

我不知道是 python 还是鳗鱼或两者都有问题。

main.py

import eel
import threading
import time

eel.init("web")

# Start the index.html file
def main():
    eel.start("index.html")

o = 0
def main0():
    global o
    while True:
        time.sleep(1)
        print(o)
        o += 10

if __name__ == '__main__':
    threading.Thread(target = main0, args=()).start()
    threading.Thread(target = main, args=()).start()

/web/index.html

<h1>
  Hello World!
</h1>

错误:-

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\inven\Desktop\ui\main.py", line 10, in main
    eel.start("index.html")
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\eel\__init__.py", line 180, in start
    run_lambda()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\eel\__init__.py", line 171, in run_lambda
    return btl.run(
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\bottle.py", line 3137, in run
    server.run(app)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\bottle_websocket\server.py", line 17, in run
    server.serve_forever()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\baseserver.py", line 398, in serve_forever
    self.start()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\baseserver.py", line 336, in start
    self.init_socket()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\pywsgi.py", line 1546, in init_socket
    self.update_environ()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\pywsgi.py", line 1558, in update_environ
    name = socket.getfqdn(address[0])
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_socketcommon.py", line 304, in getfqdn
    hostname, aliases, _ = gethostbyaddr(name)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_socketcommon.py", line 276, in gethostbyaddr
    return get_hub().resolver.gethostbyaddr(ip_address)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\hub.py", line 841, in _get_resolver
    self._resolver = self.resolver_class(hub=self) # pylint:disable=not-callable
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\resolver\thread.py", line 39, in __init__
    self.pool = hub.threadpool
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\hub.py", line 865, in _get_threadpool
    self._threadpool = self.threadpool_class(self.threadpool_size, hub=self)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\hub.py", line 860, in threadpool_class
    return GEVENT_CONFIG.threadpool
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_config.py", line 50, in getter
    return self.settings[setting_name].get()
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_config.py", line 146, in get
    self.value = self.validate(self._default())
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_config.py", line 248, in validate
    return self._import_one_of([self.shortname_map.get(x, x) for x in value])
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_config.py", line 223, in _import_one_of
    return self._import_one(candidates[-1])
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\_config.py", line 237, in _import_one
    module = importlib.import_module(module)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\threadpool.py", line 748, in <module>
    class ThreadPoolExecutor(concurrent.futures.ThreadPoolExecutor):
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\__init__.py", line 49, in __getattr__
    from .thread import ThreadPoolExecutor as te
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\thread.py", line 37, in <module>
    threading._register_atexit(_python_exit)
  File "C:\Users\inven\AppData\Local\Programs\Python\Python39\lib\threading.py", line 1394, in _register_atexit
    raise RuntimeError("can't register atexit after shutdown")
RuntimeError: can't register atexit after shutdown

Eel 文档中有一个标题为 Asynchronous Python 的部分推荐了 Eel-friendly 方法。

您的示例 re-written 遵循该方法如下所示:

import eel

eel.init("web")

o = 0

eel.start("index.html", block=False)

while True:
    eel.sleep(1)
    print(o)
    o += 10

Solution

您的代码正在启动两个线程并立即退出主线程,这会导致其余线程立即关闭。

您需要加入线程,或者使用主线程。

例如:

if __name__ == '__main__':
     threading.Thread(target = main0, args=()).start() 
     main()