Python signals: ValueError: signal only works in main thread
Question:
I have this python code:
from pytchat import LiveChat
import time
import requests
import threading
video_id = None
liveChat = None
def runChat():
global liveChat
liveChat = LiveChat(video_id)
while liveChat.is_alive():
try:
chatdata = liveChat.get()
for c in chatdata.items:
print(f"{c.datetime} [{c.author.channelId}]- {c.message}")
chatdata.tick()
except KeyboardInterrupt:
liveChat.terminate()
break
def checkId():
threading.Timer(1.0, checkId).start()
global video_id
req = requests.get("http://localhost/getId")
if req.status_code == 200:
vidId = req.text
if vidId != "null" and vidId != video_id:
video_id = vidId
print(f"got id: {video_id}")
if liveChat is not None:
liveChat.terminate()
runChat()
def main():
checkId()
if __name__ == '__main__':
main()
That utilizes the pytchat library (https://github.com/taizan-hokuto/pytchat).
As you can see, I want to dynamically change the videoId that the chat is bundled to, but with the code above this is the result:
root@vps:~# python3 liveChat.py
got id: a10TAdVZmOq # First id, the library works, we can capture chat messages.
got id: NMre6IAPoLI # After second id appears, there is this exception:
Exception in thread Thread-52:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
File "liveChat.py", line 36, in checkId
runChat()
File "liveChat.py", line 11, in runChat
liveChat = LiveChat(video_id)
File "/usr/local/lib/python3.6/site-packages/pytchat/core_multithread/livechat.py", line 110, in __init__
signal.signal(signal.SIGINT, lambda a, b: self.terminate())
File "/usr/local/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
How can this be solved since signals only work in main thread?
Answers:
Adding the parameter interruptable=False
to the LiveChat object solved the issue.
def runChat():
global liveChat
liveChat = LiveChat(video_id, interruptable=False) # <---THIS
while liveChat.is_alive():
# ....
Excellent Thanks for the fix!
I was running into this same issue trying to set up a program..
I am using pytchat as well but not the ‘livechat’ portion but instead mine is as follows..
chat = pytchat.create(video_id="jfKfPfyJRdk", interruptable=False)
but i did not have interupptable=false until i came across this post which solved my issue
I have this python code:
from pytchat import LiveChat
import time
import requests
import threading
video_id = None
liveChat = None
def runChat():
global liveChat
liveChat = LiveChat(video_id)
while liveChat.is_alive():
try:
chatdata = liveChat.get()
for c in chatdata.items:
print(f"{c.datetime} [{c.author.channelId}]- {c.message}")
chatdata.tick()
except KeyboardInterrupt:
liveChat.terminate()
break
def checkId():
threading.Timer(1.0, checkId).start()
global video_id
req = requests.get("http://localhost/getId")
if req.status_code == 200:
vidId = req.text
if vidId != "null" and vidId != video_id:
video_id = vidId
print(f"got id: {video_id}")
if liveChat is not None:
liveChat.terminate()
runChat()
def main():
checkId()
if __name__ == '__main__':
main()
That utilizes the pytchat library (https://github.com/taizan-hokuto/pytchat).
As you can see, I want to dynamically change the videoId that the chat is bundled to, but with the code above this is the result:
root@vps:~# python3 liveChat.py
got id: a10TAdVZmOq # First id, the library works, we can capture chat messages.
got id: NMre6IAPoLI # After second id appears, there is this exception:
Exception in thread Thread-52:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
File "liveChat.py", line 36, in checkId
runChat()
File "liveChat.py", line 11, in runChat
liveChat = LiveChat(video_id)
File "/usr/local/lib/python3.6/site-packages/pytchat/core_multithread/livechat.py", line 110, in __init__
signal.signal(signal.SIGINT, lambda a, b: self.terminate())
File "/usr/local/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
How can this be solved since signals only work in main thread?
Adding the parameter interruptable=False
to the LiveChat object solved the issue.
def runChat():
global liveChat
liveChat = LiveChat(video_id, interruptable=False) # <---THIS
while liveChat.is_alive():
# ....
Excellent Thanks for the fix!
I was running into this same issue trying to set up a program..
I am using pytchat as well but not the ‘livechat’ portion but instead mine is as follows..
chat = pytchat.create(video_id="jfKfPfyJRdk", interruptable=False)
but i did not have interupptable=false until i came across this post which solved my issue