套接字线程和 PyGTK
我正在尝试编写一个即时消息程序,基本的用户界面即将完成,我正在研究消息的接收部分。我有一个 UI 类和一个线程 Receive_Socket 类。每次 Received_Socket 类的套接字接收到消息时,它都会执行 gobject.idle_add() 来调用 UI 方法,以便将消息显示到聊天窗口中。在 gobject.idle.add() 行之后,我有一个 while 循环,该循环一直循环直到消息实际上显示在聊天窗口中(我希望在接收另一条消息之前显示该消息,因为我读到了 gobject.idle_add()不保证执行顺序,当然我希望消息按顺序显示:))
我尝试总结我的代码:
UI类:
Class UI:
##### somewhere in the init #####
self.message_status = 0
def chat_window(self, contact, message=0):
Check if a chat window is opened for the contact, if not it opens one.
=> In reality this check if a gtk.Window containing a gtk.Notebook is opened,
=> if not it opens one
=> Then it creates a page for the contact in the notebook, containing a
=> gtk.Textview which displays messages
if message:
self.message_display(contact, message)
def message_display(self,a,b):
=> Display the message in the message in the contact's textview
self.message_status = 1
Threaded Receive_Socket类:
Class Receive_Socket(threading.thread):
message = sock.recv(1024)
=> the sender name is written in the message
if message:
gobject.idle_add(myui.chat_window,sender, message)
while not myui.message_status:
time.sleep(0.1)
myui.message_status = 0
主要代码:
if __name__ == "__main__":
myui = UI()
reception = Receive_Socket()
reception.start()
gtk.main()
我的问题:
1)这种代码看起来高效吗?它(拥有一个线程接收类和我的 UI 类)是继续的最佳方法吗?
2)当消息显示时,socket可能已经收到了2条或更多消息,因此当再次执行message = sock.recv(1024)时,多条消息将连接到变量message中。我考虑在每条消息中包含消息长度,因此如果 1024 字节中有超过 1 条消息,它将获取该消息并将其余消息放入 message_buffer 变量中,并在再次执行 sock.recv(1024) 之前检查是否message_buffer 变量包含任何内容,如果是,则将 message_buffer 放入 message 变量中,而不是 sock.recv(1024)。有没有更简单/更好的解决方案来做到这一点?
提前致谢,
诺尔希安
I'm trying to write a instant messaging program, the basic ui is almost finished and i'm looking into the receiving part of messages. I have an UI class and a threaded Receive_Socket class. Each time the socket of the Received_Socket class receive a message, it does a gobject.idle_add() to call an UI method in order to display the message into a chat window. After the gobject.idle.add() line, i have a while loop which loops until the message is in fact displayed in the chat window ( I want the message to be displayed before receiving another message because i read that gobject.idle_add() does not guarantee execution order, and of course i want messages to be displayed in order :) )
I tried to sum up my code :
UI Class :
Class UI:
##### somewhere in the init #####
self.message_status = 0
def chat_window(self, contact, message=0):
Check if a chat window is opened for the contact, if not it opens one.
=> In reality this check if a gtk.Window containing a gtk.Notebook is opened,
=> if not it opens one
=> Then it creates a page for the contact in the notebook, containing a
=> gtk.Textview which displays messages
if message:
self.message_display(contact, message)
def message_display(self,a,b):
=> Display the message in the message in the contact's textview
self.message_status = 1
Threaded Receive_Socket class:
Class Receive_Socket(threading.thread):
message = sock.recv(1024)
=> the sender name is written in the message
if message:
gobject.idle_add(myui.chat_window,sender, message)
while not myui.message_status:
time.sleep(0.1)
myui.message_status = 0
Main Code:
if __name__ == "__main__":
myui = UI()
reception = Receive_Socket()
reception.start()
gtk.main()
My questions :
1 ) Does this kind of code seem efficient ? Is it ( Having a threaded receiving class along with my UI class ) the best way to proceed ?
2) By the time the message is displayed, the socket may have received 2 or more messages, so when it does message = sock.recv(1024) again, multiple messages will be concatenated in the variable message. I thought of including the message length in each message so if there is more than 1 message in the 1024 bytes it will take the message and put the rest in a message_buffer variable and before doing a sock.recv(1024) again it would check if the message_buffer variable contains anything and if so, put the message_buffer in the message variable instead of sock.recv(1024). Is there a easier/better solution to do this ?
Thanks in advance,
Nolhian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有。不要使用线程。相反,使用
glib.io_add_watch
使 gtk/glib 本身在套接字准备好读取时调用您的函数。这样你就不会冻结你的图形用户界面并且不需要线程。您也不需要idle_add
。如果您执行1,则不会出现此问题,因为消息将按顺序到达,并且不会出现并发线程来混淆数据。
No. Don't use threads. Instead, use
glib.io_add_watch
to make gtk/glib itself call your function when the socket is ready to read. That way you won't freeze your gui and won't need threads. You also won't needidle_add
.If you do 1, you won't have this problem, since messages will arrive in order and there will be no concurrent threads to confuse the data.