pyopengl同时作为socket服务器时显示阻塞

发布于 2024-10-08 09:50:08 字数 1177 浏览 3 评论 0原文

我实际上正在使用 pyopengl 程序充当套接字服务器。同时 服务器接收客户端的命令,解释这些命令,同时进行相应的绘制。

如果主线程在执行套接字操作时被阻塞,我实际上启动了一个线程来执行套接字服务器接受操作,并在显示循环中调用 updateserver 套接字方法。

class SocketServer(Thread):
def __init__ (self ):
    Thread.__init__(self)
    self.serversocket = socket(AF_INET, SOCK_STREAM);
    self.serversocket.bind(("127.0.0.1", 7780));
    self.status = -1;#not connected
    self.clientsocket         = None;
    self.clientaddress        = None;
    self.clientbuffer         = None;
    self.serversocket.listen(5);
def run(self):
    print 'thread running'
    while( self.status == -1 ):
        time.sleep(10);
        (self.clientsocket, self.clientaddress) = self.serversocket.accept();
        self.clientbuffer = self.clientsocket.makefile('r',0);
        self.status = 0;
    #Thread.kill();
    #print 'SERVER LISTENNING FINISHED';

def getClientContent(self):
    if ( not self.clientbuffer ): return "NONE CLIENT BUFFER";
    return self.clientbuffer.readline();

ss = SocketServer();

def updateServerSocket():
    global ss;
    print ss.getClientContent();
  1. 一旦用另一个 python 文件编写的客户端开始推送数据,我不知道如何终止线程
  2. ,显示循环挂起,就像死了一样

I am actually using a pyopengl program to act as a socket server. At the same time
the server receives commands from clients and interprets these commands and does corresponding drawing at the same time.

In case that the main thread got blocked when doing socket things I actually started a thread to do the socket server accept thing, and the updateserver socket method is called in the display loop.

class SocketServer(Thread):
def __init__ (self ):
    Thread.__init__(self)
    self.serversocket = socket(AF_INET, SOCK_STREAM);
    self.serversocket.bind(("127.0.0.1", 7780));
    self.status = -1;#not connected
    self.clientsocket         = None;
    self.clientaddress        = None;
    self.clientbuffer         = None;
    self.serversocket.listen(5);
def run(self):
    print 'thread running'
    while( self.status == -1 ):
        time.sleep(10);
        (self.clientsocket, self.clientaddress) = self.serversocket.accept();
        self.clientbuffer = self.clientsocket.makefile('r',0);
        self.status = 0;
    #Thread.kill();
    #print 'SERVER LISTENNING FINISHED';

def getClientContent(self):
    if ( not self.clientbuffer ): return "NONE CLIENT BUFFER";
    return self.clientbuffer.readline();

ss = SocketServer();

def updateServerSocket():
    global ss;
    print ss.getClientContent();
  1. I don't know how to terminate the thread
  2. once the client written in another python file started to push data, the display loop got hangup as if it were dead

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-10-15 09:50:08

在 getClientContent 中,self.clientbuffer.readline(); 行正在套接字上执行阻塞调用,这几乎肯定是导致您遇到麻烦的原因。

相反,您需要做的是让 run 方法执行读取行并将数据粘贴到队列中,然后图形显示线程从中读取数据。

In getClientContent, the line self.clientbuffer.readline(); is doing a blocking call on the socket, and that is almost certainly what is causing your trouble.

What you need to do instead is have your run method do the readlines and stick the data into a queue, which the graphical display thread then reads from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文