Hi all
I'm programming a basic IRC client, and have an IRC api/library, the Qt Designer generated interface file, and a separate module in the middle to link the two into some kind of functionality.
In the IRC library, I have a member function that checks a socket I've set up for any incoming data:
def recvLoop(self):
try:
data = self.irc.recv(4096)
if data.find('PING') != -1:
self.irc.send('PONG ' + data.split() [1] + '\r\n')
if len(data) > 0:
incomingBuffer.put(data)
#print data
#time.sleep(1)
except:
print 'Error: Possible interupt.'
def recvLoop(self):
try:
data = self.irc.recv(4096)
if data.find('PING') != -1:
self.irc.send('PONG ' + data.split() [1] + '\r\n')
if len(data) > 0:
incomingBuffer.put(data)
#print data
#time.sleep(1)
except:
print 'Error: Possible interupt.'
To copy to clipboard, switch view to plain text mode
Then in the main module (mainwindow.py) that uses the IRC library, I have the following:
@pyqtSignature("")
def on_connectButton_clicked(self):
self.displayBrowser.append('Connecting...')
client = crisp_irc.IRC()
client.newConnection('irc.oftc.net', 6667, 'crispycream', 'crisp', 'crisp')
client.join('#crasp')
client.channelSend('#crasp', 'blah')
client.set_recv_function(crisp_irc.my_receive)
while 1:
client.recvLoop()
if crisp_irc.incomingBuffer.qsize() > 0:
print crisp_irc.incomingBuffer.qsize()
item = crisp_irc.incomingBuffer.get()
#print item
self.displayBrowser.append(item)
crisp_irc.incomingBuffer.task_done()
...
if __name__ == "__main__":
import sys
form = CrispIRCMainWindow()
form.show()
app.exec_()
@pyqtSignature("")
def on_connectButton_clicked(self):
self.displayBrowser.append('Connecting...')
client = crisp_irc.IRC()
client.newConnection('irc.oftc.net', 6667, 'crispycream', 'crisp', 'crisp')
client.join('#crasp')
client.channelSend('#crasp', 'blah')
client.set_recv_function(crisp_irc.my_receive)
while 1:
client.recvLoop()
QApplication.processEvents()
if crisp_irc.incomingBuffer.qsize() > 0:
print crisp_irc.incomingBuffer.qsize()
item = crisp_irc.incomingBuffer.get()
#print item
self.displayBrowser.append(item)
crisp_irc.incomingBuffer.task_done()
QApplication.processEvents()
...
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = CrispIRCMainWindow()
form.show()
app.exec_()
To copy to clipboard, switch view to plain text mode
So if there's data, the IRC lib puts it into a (global - for now) buffer, and the mainwindow.py reads from that buffer to see if its empty or not. If it isnt, it appends it to the main display window.
The QApplication.processEvents() calls have solved half of my problem. The remaining problem is that the GUI freezes UNLESS the buffer is not empty, at which point it updates everything to how it should be, then goes straight back to being frozen/in a crash like state. The client does stay connected during this, and does receive messages etc, hence it updates when there's something to show.
How do I get rid of this behaviour (I've tried sleeping for a second etc, no dice)? Is there a better way to pass data along to the GUI from the library I've made? Many many thanks in advance!!
EDIT: Maybe there's a way to detect when there's data to be received, and only execute the function when there is something to be sent down the socket? Kind of data drive rather than a constant infinite loop that keep polling things, which is possibly the source of my problem?
Bookmarks