Python Irc Chatbot Hangs On Socket.recv After Seemingly Random Time Even Though Socket.settimeout Is 8
Hey so I decided to create an IRC ChatBot whose sole purpose it is to read incoming messages from Twitch Chat and if a giveaway is recognized by a keyword it's supposed to enter th
Solution 1:
I think you've missunderstood how timeout work on the socket.
s.settimeout(8.0)
Will only set s.connect(...)
to timeout if it can't reach the destination host.
Further more, usually what you want to use instead if s.setblocking(0)
however this alone won't help you either (probably).
Instead what you want to use is:
importselect
ready = select.select([s], [], [], timeout_in_seconds)
if ready[0]:
data = s.recv(1024)
What select does is check the buffer to see if any incoming data is available, if there is you call recv()
which in itself is a blocking operation. If there's nothing in the buffer select
will return empty and you should avoid calling recv()
.
If you're running everything on *Nix you're also better off using epoll.
fromselect import epoll, EPOLLIN
poll = epoll()
poll.register(s.fileno(), EPOLLIN)
events = poll.poll(1) # 1 sec timeoutfor fileno, eventin events:
ifeventis EPOLLIN and fileno == s.fileno():
data = s.recv(1024)
This is a crude example of how epoll could be used. But it's quite fun to play around with and you should read more about it
Post a Comment for "Python Irc Chatbot Hangs On Socket.recv After Seemingly Random Time Even Though Socket.settimeout Is 8"