My Python Twitch IRC bot is slow to respond - need assistance improving speed

I’m currently developing my first significant project using Python, which is an IRC bot designed for Twitch chats. While it operates, it seems to be quite slow in processing incoming messages, and I’m having trouble understanding what’s causing the delay.

import re
import socket
import threading
import time

SERVER = "irc.twitch.tv"
PORT_NUM = 6667
BOT_NAME = ""
AUTH_TOKEN = ""
CHANNEL = ""

MSG_RATE = (15/25)
MSG_PATTERN = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")

def send_message(connection, text):
    connection.send("PRIVMSG #{} :{}".format(CHANNEL, text))

# Main chat connection
main_conn = socket.socket()
main_conn.connect((SERVER, PORT_NUM))
main_conn.send("PASS {}\r\n".format(AUTH_TOKEN).encode("utf-8"))
main_conn.send("NICK {}\r\n".format(BOT_NAME).encode("utf-8"))
main_conn.send("JOIN {}\r\n".format(CHANNEL).encode("utf-8"))

# Whisper connection
whisper_conn = socket.socket()
whisper_conn.connect((SERVER, PORT_NUM))
whisper_conn.send("PASS {}\r\n".format(AUTH_TOKEN).encode("utf-8"))
whisper_conn.send("NICK {}\r\n".format(BOT_NAME).encode("utf-8"))
whisper_conn.send("CAP REQ :twitch.tv/tags twitch.tv/commands {}\r\n".format(CHANNEL).encode("utf-8"))

while True:
    chat_data = main_conn.recv(1024).decode("utf-8")
    whisper_data = whisper_conn.recv(1024).decode("utf-8")
    
    if whisper_data == "PING :tmi.twitch.tv\r\n":
        whisper_conn.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
    else:
        whisper_user = re.search(r"\w+", whisper_data).group(0)
        whisper_text = MSG_PATTERN.sub("", whisper_data)
        print(whisper_user + ": " + whisper_text)
    
    if chat_data == "PING :tmi.twitch.tv\r\n":
        main_conn.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
    else:
        chat_user = re.search(r"\w+", chat_data).group(0)
        chat_text = MSG_PATTERN.sub("", chat_data)
        print(chat_user + ": " + chat_text)
        time.sleep(1 / MSG_RATE)

To manage both regular chat messages and whispers, I’ve implemented two socket connections based on the recommendations I’ve seen. Since this is my first time working on such a complex Python program, I may be overlooking some simple solutions. Could you share any insights on what may be affecting the response speed?

Your bottleneck is those sequential recv() calls in the while loop. You’re polling both sockets one after another, so when one has no data, it blocks everything. Your bot just sits there waiting for data that might not show up for seconds. Don’t use two separate connections like this - go with asyncio and async/await patterns, or use proper threading where each connection gets its own thread. That MSG_RATE sleep is also screwing you over since it runs whether you’re sending messages or not. Only put that sleep in your message sending logic when you actually respond to commands. Right now you’re treating every loop like you’re sending a message when mostly you’re just reading.

Your blocking recv() calls are killing performance. When you call recv() on both connections one after another, the second call sits there waiting if there’s no data ready. This creates huge delays. Use threading for each connection or switch to select() for non-blocking socket polling. Also, your regex searches will crash the whole thing when patterns don’t match. Wrap those in try-except blocks or your bot’s gonna break randomly.

yeah, your recv() is blocking, which slows things down when there’s no data. try using select() or set a timeout on your sockets. also, that sleep at the end really drags response times, consider moving it or just removing it.