ValueError: signal only works in main thread when using Stomp

Question:

When I run this code I get this error:

ValueError: signal only works in main thread

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', id=1, 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")
Asked By: joes

||

Answers:

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.

Answered By: Justin Bertram

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.

Answered By: uuu777

The problem fixed after I installed:

pip3 install "pyzmq==17.0.0" "ipykernel==4.8.2"
Answered By: ah bon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.