Python signal only works in main thread

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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.

Читайте также:  Api прогноз погоды python

Already on GitHub? Sign in to your account

Exception ‘signal only works in main thread’ when Connection.run command with pty=True #2204

Exception ‘signal only works in main thread’ when Connection.run command with pty=True #2204

Comments

Describe the bug
When running a command with Connection.run and argument pty=True in a thread we get that error. fabric version 2.7.0

To Reproduce
Steps to reproduce the behaviour (please attach a minimal example):
Running the code below on fabric 2.6.0 everything work fine, while if it is run with 2.7.0 it fails due to this line

from fabric import Connection import threading def function(connection): command="uptime" connection.run(command, warn=True, pty=True, hide=False, timeout=None) connection_kwargs = < "host": MY_IP>, "user": MY_USER>, "forward_agent": False, "connect_kwargs": "key_filename": "">, > connection = Connection(**connection_kwargs) command="uptime" connection.run(command, warn=True, pty=True, hide=False, timeout=None) t1=threading.Thread(target=function, args=(connection,)) t1.start() t1.join()

Expected behaviour
fabric works within a thread

Environment
Bug seen in python 3.7.2 and 3.10.1

  • What version of the Python interpreter are you using? Are you using an
    alternative interpreter such as PyPy?
  • What operating system are you using both client & server-side? Ubuntu, centos and amazon linux as server and mac as client
  • Are you using OpenSSH server or something else? OpenSSH
  • Which version or versions of the software are you using?
    • Have you already tried the latest release? yes
    • Have you, or can you, try some older releases to pin down where the bug
      appeared? 2.6.0 work fine
     13:28:46 up 3 days, 20:17, 1 user, load average: 0.00, 0.00, 0.00 Exception in thread Thread-5 (function): Traceback (most recent call last): File "/Users/giordafr/.pyenv/versions/3.10.1/lib/python3.10/threading.py", line 1009, in _bootstrap_inner self.run() File "/Users/giordafr/.pyenv/versions/3.10.1/lib/python3.10/threading.py", line 946, in run self._target(*self._args, **self._kwargs) File "/tmp/test.py", line 5, in function connection.run(command, warn=True, pty=True, hide=False, timeout=None) File "", line 2, in run File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/fabric/connection.py", line 30, in opens return method(self, *args, **kwargs) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/fabric/connection.py", line 725, in run return self._run(self._remote_runner(), command, **kwargs) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/invoke/context.py", line 102, in _run return runner.run(command, **kwargs) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/fabric/runners.py", line 72, in run return super(Remote, self).run(command, **kwargs) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/invoke/runners.py", line 379, in run return self._run_body(command, **kwargs) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/invoke/runners.py", line 430, in _run_body self.start(command, self.opts["shell"], self.env) File "/Users/giordafr/.pyenv/versions/parallel-cluster-tests/lib/python3.10/site-packages/fabric/runners.py", line 46, in start signal.signal(signal.SIGWINCH, self.handle_window_change) File "/Users/giordafr/.pyenv/versions/3.10.1/lib/python3.10/signal.py", line 47, in signal handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler)) ValueError: signal only works in main thread of the main interpreter 

    The text was updated successfully, but these errors were encountered:

    Источник

    Saved searches

    Use saved searches to filter your results more quickly

    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

    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

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

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

    Comments

    Checklist

    • I have searched the existing issues for similar issues.
    • I added a very descriptive title to this issue.
    • I have provided sufficient information below to help reproduce this issue.

    Summary

    File «/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py», line 563, in _run_script
    exec(code, module.dict)
    File «/app/soojh/testing.py», line 13, in
    app.run()
    File «/app/soojh/pyrogram/methods/utilities/run.py», line 86, in run
    run(idle())
    File «/usr/local/lib/python3.9/asyncio/base_events.py», line 647, in run_until_complete
    return future.result()
    File «/app/soojh/pyrogram/methods/utilities/idle.py», line 83, in idle
    signal_fn(s, signal_handler)
    File «/usr/local/lib/python3.9/signal.py», line 56, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))

    Reproducible Code Example

    from pyrogram import Client, filters from pyrogram.types import Message app = Client("my client account") @app.on_message(filters.regex("Hi")) async def pdf_photo(client:Client,message:Message): await app.send_message("me","👋 Hi!") app.run()

    Источник

    Valueerror: signal only works in main thread

    valueerror signal only works in main thread

    One of the error that developers often encounter is the ValueError: signal only works in main thread error.

    This error occurs when a signal is occur in a thread that is not the main thread.

    In this article, we will discuss this error in detail and provide example codes and solutions to help you resolve it.

    What is the ValueError signal only works in main thread error?

    The ValueError: signal only works in main thread error is a common error encountered by developers when working with threads in Python.

    This error is occur when a signal is attempted to be processed in a thread other than the main thread.

    Python’s signal handling structure is designed to work specifically in the main thread, and any attempt to handle signals in a different thread will result in this error.

    Understanding threads in Python

    In simple words, a thread can be considered as a separate flow of execution within a program. Python provides a built-in threading module that allows developers to create and manage threads.

    Causes of the ValueError

    The main cause of the ValueError: signal only works in main thread error is an attempt to handle signals in a thread other than the main thread.

    How the Error Reproduce?

    Let’s take a look at an example code that demonstrates of how the valueerror occur.

    import threading import signal def example_worker_thread(): # Simulating some work print("Worker thread executing. ") # Raising a signal in the worker thread signal.raise_signal(signal.SIGINT) def main_example(): # Creating a worker thread thread_keys = threading.Thread(target=example_worker_thread) # Starting the worker thread thread_keys.start() # Waiting for the worker thread to finish thread_keys.join() if __name__ == "__main__": main_example() 

    In the above example, we make a worker thread using the threading module.

    Within the worker thread, we try to raise a SIGINT signal using the signal.raise_signal() method.

    However, since the signal is raised in a non-main thread, it results in the ValueError.

    How to fix the ValueError signal only works in main thread error?

    To fix the ValueError, we need to ensure that signals are handled only in the main thread.

    Here are a few solutions to fix this value error:

    Solution 1: Restructure the code to handle signals in the main thread

    One way to avoid the ValueError is to restructure the code that the signals are handled only in the main thread.

    This can be done by moving the signal handling code to the main thread and using thread-safe structure to communicate between the main thread and other threads.

    Solution 2: Use inter-thread communication structure

    Another solution to solve the error is to utilize inter-thread communication structure provided by Python’s threading module.

    These structure, such as Event, Lock, and Queue, can be used to safely communicate between threads without the need for signal handling.

    By applying these structure, you can prevent the ValueError altogether.

    Solution 3: Consider using alternative libraries or modules

    When handling signals in non-main threads is an important requirement for your application, you can use an alternative libraries or modules that provide support for this functionality.

    For example, the multiprocessing module in Python allows handling signals in child processes, which can be an alternative solution thats depends on your specific use case.

    FAQs

    No, Python’s signal handling structure is designed to work specifically in the main thread. If you are trying to handle signals in threads other than the main thread will result in the ValueError.

    o avoid the ValueError signal only works in main thread error, ensure that signals are handled only in the main thread.

    Conclusion

    In this article, we discussed the causes of this error and provided example codes and solutions to help you fixed it.

    By following the suggested solutions and understand the limitations of signal handling in non-main threads, you can write more solid and error-free Python programs.

    Additional Resources

    Источник

    ValueError: signal only works in main thread

    This problem doesn’t have anything to do with ActiveMQ. You’re misusing signals. As the Python documentation states:

    Python signal handlers are always executed in the main Python thread, even if the signal was received in another thread. This means that signals can’t be used as a means of inter-thread communication. You can use the synchronization primitives from the threading module instead.

    Besides, only the main thread is allowed to set a new signal handler.

    I believe the on_message method is executed in its own thread since the message is received asynchronously.

    In short, don’t use signals from threads. Use the synchronization primitives from the threading module instead. If you can’t avoid using signals then receive the STOMP message synchronously on the main thread instead of using a ConnectionListener .

    Solution 2

    I fixed it in python3.7.3 by by applying this patch manually to /usr/lib/python3.7/pdb.py

    https://github.com/python/cpython/commit/8d64bfafdffd9f866bb6ac2e5b4c4bdfcb16aea0 

    I suppose that there are better ways to achieve the same (say, pass a patched file as a parameter) without patching python library, but this one seems good enough.

    It will be really nice if this patch will be back ported to popular releases.

    joes

    Comments

    I’m using ActiveMQ. RealTor is the name where i have my Spider(selogerSpider) this one help me for the scraping.

     import stomp from RealTor import selogerSpider from scrapy.crawler import CrawlerProcess from scrapy.settings import Settings class MyListener(stomp.ConnectionListener): def on_error(self, headers, message): print('received an error "%s"' % message) def on_message(self, headers, message): print('received a message "%s"' % message) settings = Settings() settings.set("USER_AGENT", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36") settings.set("LOG_ENABLED", False) crawler = CrawlerProcess(settings) crawler.crawl(selogerSpider) selogerSpider.signals.engine_started() print("STARTING scraping") crawler.start() print("Scraping STOPPED") try: conn = stomp.Connection() conn.set_listener('', MyListener()) conn.start() """ conn = stomp.Connection([('0.0.0.0', 61613)])""" conn.connect('admin', 'password', wait=True) conn.subscribe(destination='/queue/test', ack='auto') print('subscripe') """conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')""" input("coucou") """conn.disconnect()""" print('end') except IOError as e: print("error message") 

    Источник

Оцените статью