I've read a lot of documentation about Threading, Queuing, Pooling etc. but still couldn't figure out how to solve my problem. Here's the situation : I built a python3 Django application that's served by cherrypy. The application is basically another IRC client. When I use the GUI to run my code the first time, an IRC bot is launched through a deamon Thread and listens to events. My problem is the following : how do I send data to that thread (and my bot), for instance to tell him to join a second channel ? When I run my code a second time, obviously a new instance of my bot is created, along with a new server connection. I need a way to communicate with my bot through GUI interaction. Right now the only way I had to make my bot react the specific things is by reading the database. Some other GUI action would change that database. Which is a bad system.
Here is the relevant code that starts my bot.
def DCC_deamonthread(c, server, nickname, upload):
try:
c.connect(server, 6667, nickname)
c.start()
except irc.client.ServerConnectionError as x:
log("error" + str(x)).write()
upload.status, upload.active = "Error during connection", False
upload.save()
def upload_file(filename, rec_nick, pw):
global upload
Upload_Ongoing.objects.all().delete()
upload = Upload_Ongoing(filename=filename,status="Connecting to server...", active=True)
upload.save()
irc.client.ServerConnection.buffer_class.encoding = 'latin-1'
c = DCCSend(filename, rec_nick, pw)
server = "irc.rizon.net"
nickname = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
t = threading.Thread(target=DCC_deamonthread, args=(c, server, nickname, upload))
t.daemon=True
t.start()
The problem is, as you noticed, that you spawn a new thread/bot each time there is an upload. A possible solution would be to rewrite your code to do something like this: