为什么我的python线程互相阻塞

时间:2014-10-12 00:07:33

标签: python multithreading while-loop

我目前的困境是我试图阻止网络服务脚本无阻塞,允许在任何时间进行多次下载,但目前它会挂起并等待第一次下载完成后再开始第二次下载。在你开始投票之前,因为答案是可恶的,请知道这是我的第一个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

我认为问题可以通过改变而不是一次循环3次来解决。

1 个答案:

答案 0 :(得分:0)

您应该传递对您希望在线程中启动的方法的引用。相反,您正在调用线程,并将从该方法返回的数据传递给threading.Thread()调用。

简而言之,您的代码应该成为:

     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

请注意self.ConnectionProcessorA之后删除括号等。这会传递对线程中启动方法的引用,线程模块将调用该方法。

注意,建议存储对您创建的线程的引用,以便不会收集垃圾。因此我建议您的代码变为:

     if (A==0):
         self.cpa_thread= threading.Thread(target=self.ConnectionProcessorA)
         self.cpa_thread.start()
         A=1
     elif (A==1):
         self.cpb_thread= threading.Thread(target=self.ConnectionProcessorB)
         self.cpb_thread.start()
         A=2
     else:
         self.cpc_thread= threading.Thread(target=self.ConnectionProcessorC)
         self.cpc_thread.start()
         A=0