pythonpython-3.xmultithreadingpython-multithreadingeel

Python: running eel with multithreading results in a error


NOTE:-

  1. if run eel without threading, it works just fine
  2. if i run threading without eel, it works just fine
  3. if i use Multiprocessing with eel, it works just fine
  4. i have tried uninstalling and reinstalling python several times, i have also tried uninstalling and reinstalling eel several times, but with no success so far...

i don't know if its a problem with python or eel or both.

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>

Error:-

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

Solution

  • Solution

    Your code is starting two threads and immediately exits main thread, which causes the rest of the threads to shutdown immediately.

    You need to either join on the threads, or use the main thread for something.

    for example:

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