pyopengl同时作为socket服务器时显示阻塞
我实际上正在使用 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();
- 一旦用另一个 python 文件编写的客户端开始推送数据,我不知道如何终止线程
- ,显示循环挂起,就像死了一样
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();
- I don't know how to terminate the thread
- once the client written in another python file started to push data, the display loop got hangup as if it were dead
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 getClientContent 中,self.clientbuffer.readline(); 行正在套接字上执行阻塞调用,这几乎肯定是导致您遇到麻烦的原因。
相反,您需要做的是让 run 方法执行读取行并将数据粘贴到队列中,然后图形显示线程从中读取数据。
In
getClientContent
, the lineself.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.