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



注意:-

  1. 如果在没有穿线的情况下运行鳗鱼,效果很好
  2. 如果我在没有鳗鱼的情况下穿线,效果很好
  3. 如果我在eel中使用多处理器,它运行得很好
  4. 我试过几次卸载和重新安装python,也试过好几次卸载并重新安装eel,但到目前为止都没有成功

我不知道是python还是eel的问题,或者两者都有。

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:UsersinvenAppDataLocalProgramsPythonPython39libthreading.py", line 973, in _bootstrap_inner
self.run()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libthreading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:UsersinvenDesktopuimain.py", line 10, in main
eel.start("index.html")
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packageseel__init__.py", line 180, in start
run_lambda()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packageseel__init__.py", line 171, in run_lambda
return btl.run(
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesbottle.py", line 3137, in run
server.run(app)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesbottle_websocketserver.py", line 17, in run
server.serve_forever()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventbaseserver.py", line 398, in serve_forever
self.start()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventbaseserver.py", line 336, in start
self.init_socket()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventpywsgi.py", line 1546, in init_socket
self.update_environ()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventpywsgi.py", line 1558, in update_environ
name = socket.getfqdn(address[0])
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_socketcommon.py", line 304, in getfqdn
hostname, aliases, _ = gethostbyaddr(name)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_socketcommon.py", line 276, in gethostbyaddr
return get_hub().resolver.gethostbyaddr(ip_address)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventhub.py", line 841, in _get_resolver
self._resolver = self.resolver_class(hub=self) # pylint:disable=not-callable
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventresolverthread.py", line 39, in __init__
self.pool = hub.threadpool
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventhub.py", line 865, in _get_threadpool
self._threadpool = self.threadpool_class(self.threadpool_size, hub=self)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventhub.py", line 860, in threadpool_class
return GEVENT_CONFIG.threadpool
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_config.py", line 50, in getter
return self.settings[setting_name].get()
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_config.py", line 146, in get
self.value = self.validate(self._default())
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_config.py", line 248, in validate
return self._import_one_of([self.shortname_map.get(x, x) for x in value])
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_config.py", line 223, in _import_one_of
return self._import_one(candidates[-1])
File "C:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgevent_config.py", line 237, in _import_one
module = importlib.import_module(module)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libimportlib__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:UsersinvenAppDataLocalProgramsPythonPython39libsite-packagesgeventthreadpool.py", line 748, in <module>
class ThreadPoolExecutor(concurrent.futures.ThreadPoolExecutor):
File "C:UsersinvenAppDataLocalProgramsPythonPython39libconcurrentfutures__init__.py", line 49, in __getattr__
from .thread import ThreadPoolExecutor as te
File "C:UsersinvenAppDataLocalProgramsPythonPython39libconcurrentfuturesthread.py", line 37, in <module>
threading._register_atexit(_python_exit)
File "C:UsersinvenAppDataLocalProgramsPythonPython39libthreading.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友好的方法。

按照这种方法重新编写的示例如下所示:

import eel
eel.init("web")
o = 0
eel.start("index.html", block=False)
while True:
eel.sleep(1)
print(o)
o += 10

解决方案

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

您需要在线程上联接,或者使用主线程来执行某些操作。

例如:

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