Answer a 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")

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.

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐