【发布时间】:2014-10-12 00:07:32
【问题描述】:
我目前的困境是我试图让一个阻塞的网络服务脚本成为非阻塞的,以允许在任何时候进行多个下载,但目前它会挂起并等待第一个下载完成,然后再开始第二个.在你不遗余力地投票之前,因为答案很可恶,请知道这是我的第一个 python 脚本,我是自学的。
在下面的示例中,我只发布了一个“ConnectionProcesser”,因为它们都包含相同的代码 如果您需要更多代码,请询问
脚本有3个依赖项
import socket # Networking support
import signal # Signal support (server shutdown on signal receive)
import threading #to make the thing run more than one at a time
请注意,脚本已经过编辑,缺少相当多的代码,但我认为这与问题无关。
def ConnectionProcessorC(self):
connC, AddressC = self.socket.accept()
print("C Got connection from:", AddressC)
DataRecivedC = connC.recv(1024) #receive data from client
DataRecivedC = bytes.decode(DataRecivedC) #decode it to string
print(DataRecivedC)
RequestMethod = DataRecivedC.split(' ')[0]
print ("C Method: ", RequestMethod)
if (RequestMethod == 'GET') | (RequestMethod == 'HEAD'):
Response_Headers = 'HTTP/1.1 200 OK\n'
# Current_Date = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
# Response_Headers += 'Date: ' + current_date +'\n'
Response_Headers += 'Server: Moes-Python-Server\n'
Response_Headers += 'Connection: close\n\n' # signal that the conection wil be closed after complting the request
Server_Response = Response_Headers.encode() # return headers for GET and HEAD
file_handler = open('/usr/share/nginx/html/100mb.dump','rb')
Response_Content = file_handler.read() # read file content
file_handler.close()
URL=DataRecivedC.split(' ')
URL = URL[1] # get 2nd element
#Response_Content="<html><body><p>Charlie TEStin this stuff yehURL:"+URL+"</p></body></html>"
Server_Response += Response_Content
connC.send(Server_Response)
print ("C Closing connection with client")
else:
print("C Unknown HTTP request method:", RequestMethod)
connC.close()
return
def Distrabuteconnections(self):
A=0
""" Main loop awaiting connections """
while True:
print ("Awaiting New connection")
self.socket.listen(10) # maximum number of queued connections #changed to 1 from 3 to try and prevent waiting after closing for ther que to clean up
if (A==0):
ConnectionProcessorA = threading.Thread(target=self.ConnectionProcessorA())
ConnectionProcessorA.start()
A=1
elif (A==1):
ConnectionProcessorB = threading.Thread(target=self.ConnectionProcessorB())
ConnectionProcessorB.start()
A=2
else:
ConnectionProcessorC = threading.Thread(target=self.ConnectionProcessorC())
ConnectionProcessorC.start()
A=0
我认为可以通过将 while true 更改为循环 3 次而不是 1 次的东西来解决问题。
【问题讨论】:
-
从线程切换到子进程应该可以解决它
-
但是为什么我需要子流程是有原因的吗?他们跑的不是更慢吗?
-
子进程可能需要更长的时间才能启动并且具有更高的内存占用,但没有理由让它们变慢。原因是 GIL,它似乎会干扰线程。这不会发生在子流程中。
-
不会影响线程之间共享的全局变量你知道像我这样的人可以从哪里开始将其转换为子进程吗?一些链接或需要做什么的简短示例会很好,我认为我仍然可以和朋友一起做这个,所以我将保留这个问题并希望有人提出解决方案
-
仅供参考,对于以后遇到这个问题的人来说,这里的问题与 GIL 无关。
标签: python multithreading while-loop