Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create Client object when using threading #163

Closed
rogerio-alves opened this issue Feb 13, 2020 · 3 comments
Closed

Cannot create Client object when using threading #163

rogerio-alves opened this issue Feb 13, 2020 · 3 comments
Assignees
Labels

Comments

@rogerio-alves
Copy link

Hi!

I'm using socketio in a multithreaded app using the threading module. When updating from python-engineio 3.11.1 to python-engineio 3.11.2, there is an apparent issue with initializing the SIGINT signal handler when the Client object is created. This happens only if it is made outside the main thread, as suggested by the official python docs. The exception launched is:

ValueError: signal only works in main thread

I've seen that the initialization was made only if the file was running as __main__ in previous versions and was changed due to issue #147 .
Is it considered a bad practice to use socketio in a multithreaded app? Or is there any reason to only use it in the main thread?

The full traceback is as follows:

Traceback (most recent call last):
  File "/home/rogerio/git/proj/threads/communication.py", line 59, in event_client_thread
    sio = socketio.Client()
  File "/home/rogerio/.local/lib/python3.6/site-packages/socketio/client.py", line 97, in __init__
    self.eio = self._engineio_client_class()(**engineio_options)
  File "/home/rogerio/.local/lib/python3.6/site-packages/engineio/client.py", line 82, in __init__
    signal_handler)
  File "/usr/lib/python3.6/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
@miguelgrinberg miguelgrinberg self-assigned this Feb 13, 2020
@miguelgrinberg
Copy link
Owner

I'll look into it. The change that I've made in 3.11.2 was to address a similar error, it seems I did not cover all possibilities with my fix.

@miguelgrinberg
Copy link
Owner

@rogerio-alves I have changed it so that the signal handler is only configured when the client lives in the main thread.

The downside is that Ctrl-C will not provide clean up when you have your client in another thread.

Can you give the master branch of this repo a try, and confirm that you do not get the error anymore?

@rogerio-alves
Copy link
Author

@miguelgrinberg It did work! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants