gpt4 book ai didi

python - 值错误: signal only works in main thread when using Stomp

转载 作者:行者123 更新时间:2023-12-01 08:19:31 27 4
gpt4 key购买 nike

当我运行此代码时,出现此错误:

ValueError: signal only works in main thread

我正在使用 ActiveMQ。

RealTor 是我的 Spider(selogerSpider) 的名称,它可以帮助我进行抓取。

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

最佳答案

这个问题与ActiveMQ没有任何关系。你滥用信号。作为Python documentation状态:

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.

我相信 on_message 方法是在自己的线程中执行的,因为消息是异步接收的。

简而言之,不要使用来自线程的信号。使用 threading module 中的同步原语反而。如果您无法避免使用信号,则在主线程上同步接收 STOMP 消息,而不是使用 ConnectionListener

关于python - 值错误: signal only works in main thread when using Stomp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com