套接字线程和 PyGTK [英] Socket Thread and PyGTK

查看:39
本文介绍了套接字线程和 PyGTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个即时消息程序,基本的 ui 即将完成,我正在研究消息的接收部分.我有一个 UI 类和一个线程化的 Receive_Socket 类.Received_Socket 类的套接字每次收到消息时,都会执行 gobject.idle_add() 来调用 UI 方法,以便将消息显示到聊天窗口中.在 gobject.idle.add() 行之后,我有一个 while 循环,循环直到消息实际上显示在聊天窗口中(我希望在收到另一条消息之前显示该消息,因为我阅读了 gobject.idle_add()不保证执行顺序,当然我希望消息按顺序显示:) )

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 :) )

我试图总结我的代码:

界面类:

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 类:

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

主要代码:

if __name__ == "__main__":
    myui = UI()
    reception = Receive_Socket()
    reception.start()
    gtk.main()

我的问题:

1 ) 这种代码看起来有效吗?这是(在我的 UI 类中使用线程接收类)是最好的方法吗?

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) 到消息显示时,socket 可能已经收到了 2 条或更多条消息,所以当它再次执行 message = sock.recv(1024) 时,多个消息将在变量 message 中连接起来.我想在每条消息中包含消息长度,因此如果 1024 字节中有超过 1 条消息,它将获取该消息并将其余消息放入 message_buffer 变量中,然后再次执行 sock.recv(1024) 之前,它会检查是否message_buffer 变量包含任何内容,如果是,请将 message_buffer 放在 message 变量中,而不是 sock.recv(1024).有没有更简单/更好的解决方案来做到这一点?

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 ?

提前致谢,

诺利安

推荐答案

  1. 没有.不要使用线程.相反,使用 glib.io_add_watch 使 gtk/glib 在套接字准备好读取时调用您的函数.这样你就不会冻结你的 gui 并且不需要线程.您也不需要 idle_add.

如果你做1,你就不会有这个问题,因为消息会按顺序到达,并且不会有并发线程来混淆数据.

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.

这篇关于套接字线程和 PyGTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆