python SocketServer UDP接收forking问题 ,这个错在哪里?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python SocketServer UDP接收forking问题 ,这个错在哪里?相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
from SocketServer import (UDPServer as UDP,
DatagramRequestHandler as DRH,
ForkingMixIn as FMI)
from time import ctime
HOST = ''
PORT = 8000
ADDR = (HOST, PORT)
class Server(FMI, UDP):
pass
class MyRequestHandler(DRH):
def handle(self):
print '...connected from:', self.client_address
self.wfile.write('[%s] %s' % (ctime(), self.rfile.readline()))
udpServ = Server(ADDR, MyRequestHandler)
print 'waiting for connection...'
udpServ.serve_forever()
运行结果:
waiting for connection...
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 65066)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 284, in _handle_request_nobloc
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 545, in process_request
pid = os.fork()
AttributeError: 'module' object has no attribute 'fork'
如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择。由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序?
由于Python是跨平台的,自然也应该提供一个跨平台的多进程支持。multiprocessing模块就是跨平台版本的多进程模块。
multiprocessing模块提供了一个Process类来代表一个进程对象,下面的例子演示了启动一个子进程并等待其结束:
from multiprocessing import Processimport os
# 子进程要执行的代码
def run_proc(name):
print 'Run child process %s (%s)...' % (name, os.getpid())
if __name__=='__main__':
print 'Parent process %s.' % os.getpid()
p = Process(target=run_proc, args=('test',))
print 'Process will start.'
p.start()
p.join()
print 'Process end.'
执行结果如下:
Parent process 928.Process will start.
Run child process test (929)...
Process end.
创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动,这样创建进程比fork()还要简单。
join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。
参考技术A os.fork()在windows环境下已经没法用了,在linux下试试吧。Unfortunately, this won't work on Windows in standard Python today; fork is too much at odds with the Windows model, and a port of this call is still in the works (see also this chapter's sidebar about Cygwin Pythonyou can fork with Python on Windows under Cygwin, but it's not exactly the same). Because forking is ingrained in the Unix programming model, though, this script works well on Unix, Linux, and modern Macs追问
windows下,要实现类似功能,有什么其他办法吗?
基于upd的socketserver,即udp的多线程
服务端
#udp服务端多进程
import socketserver
class My_server(socketserver.BaseRequestHandler):
def handle(self):
#while True:
data =self.request[0]
print(data.decode(‘utf-8‘))
print(self.client_address,self.request[1])
#mag =input(‘>>>:‘).strip()
#self.request[1].sendto(mag.encode(‘utf-8‘),self.client_address)
# print(self.request)
# print(self.client_address)
self.request[1].sendto(‘bbb‘.encode(‘utf-8‘), self.client_address)
if __name__ == ‘__main__‘:
ip_port =(‘127.0.0.1‘,8083)
obj =socketserver.ThreadingUDPServer(ip_port,My_server)#买手机,绑定手机卡
obj.serve_forever()#提供了一个循环
客户端
import socket
updclient =socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #买手机
server_ip_port =(‘127.0.0.1‘,8083) #找到服务端软件
while True:#通信循环
msg =input(‘>>>:‘).strip()
updclient.sendto(msg.encode(‘utf-8‘),server_ip_port) #发送信息,信息量,服务端地址
data,server_ip =updclient.recvfrom(1024) #接收信息
print(data.decode(‘utf-8‘))
以上是关于python SocketServer UDP接收forking问题 ,这个错在哪里?的主要内容,如果未能解决你的问题,请参考以下文章