Search code examples
pythonvalueerrorfletultralytics

ValueError: signal only works in main thread of the main interpreter


I am developing an app for realtime object detection that gets opened by voice commands.. There is no error in the program too, but the thing is everytime i run the program flet window says this

ValueError: signal only works in main thread of the main interpreter

but the thing is that i can see the objects which i have added in the background.. I dont know why i am getting this error and i have never got it before, please HELP as i need to complete my project in 2 days

CLICK FOR SOURCE CODE

source code is linked so that you can see what is wrong please help me


Solution

  • I downloaded your source code, compiled the right version of Python, installed all the prerequisites, and was able to reproduce your problem.

    In the main() function, you have this line:

        app(target=main)
    

    But you also have this line at the end:

    if __name__ == "__main__":
        app(target=main)
    

    which means that it is attempting to call itself. If this had worked, you would have gotten infinite recursion and stack overflow. Delete the first app(target=main).

    What was happening is that the flet library was trying to register signal handlers (a signal is an event that happens to a program, like a ^C, a segmentation fault, a hangup, a pipe breaking, etc.) but this has to be done in the main Python thread. When you called app(), it was setting up a signal handler and creating another thread of execution for main(), then executed main(), which called app() again, which attempted to set up signal handlers again (and would have called main(), until you ran out of memory), but this failed because you were in a thread, not in the main line of execution.

    You got a traceback on the screen when this problem happened; when you post a question, be sure to post this traceback, e.g.:

      File "/home/vercingatorix/src/git/Drusthi/test.py", line 86, in main
        app(target=main)
      File "/home/vercingatorix/src/git/Drusthi/so2/lib/python3.11/site-packages/flet_runtime/app.py", line 70, in app
        return asyncio.run(
               ^^^^^^^^^^^^
      File "/home/vercingatorix/.pyenv/versions/3.11.9/lib/python3.11/asyncio/runners.py", line 190, in run
        return runner.run(main)
               ^^^^^^^^^^^^^^^^
      File "/home/vercingatorix/.pyenv/versions/3.11.9/lib/python3.11/asyncio/runners.py", line 118, in run
        return self._loop.run_until_complete(task)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/vercingatorix/.pyenv/versions/3.11.9/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
        return future.result()
               ^^^^^^^^^^^^^^^
      File "/home/vercingatorix/src/git/Drusthi/so2/lib/python3.11/site-packages/flet_runtime/app.py", line 148, in app_async
        signal.signal(signal.SIGINT, exit_gracefully)
      File "/home/vercingatorix/.pyenv/versions/3.11.9/lib/python3.11/signal.py", line 58, in signal
        handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ValueError: signal only works in main thread of the main interpreter
    

    This is essential in debugging the problem.

    If this solves your problem to your satisfaction, please mark the answer as Accepted! Thank you in advance; it is much appreciated!