2014-12-22 44 views
1

我需要运行一次处理最多3个请求的服务器。我的设计是我有一个TCP服务器运行,并将有3个线程运行来处理这些请求。服务器将接受请求,并将这些请求传递给使用锁的相应队列的线程。我也有适合队列的锁。我的问题是,尽管我有一个信号处理程序在主进程不得不退出时用信号通知线程,使用一个标志。我不理解错误是什么,因为优雅的退出没有像预期的那样发生。输出如下:TCP线程python服务器没有按预期处理信号

vm:~/Desktop$ python multi_threaded_queueing.py 
About to kickoff 
About to kickoff 
Starting Thread-1 
About to kickoff 
Starting Thread-2 
Starting Thread-3 
^CTraceback (most recent call last): 
    File "multi_threaded_queueing.py", line 94, in <module> 
    conn, addr = s.accept() 
    File "/usr/lib/python2.7/socket.py", line 202, in accept 
    sock, addr = self._sock.accept() 
socket.error: [Errno 4] Interrupted system call 

的代码如下:

#!/usr/bin/python 

import Queue 
import threading 
import time 
import sys 
import socket 
import signal 


HOST = '127.0.0.1' 
PORT = 50007    # Arbitrary non-privileged port 
s = None 

exitFlag = 0 
#signal handler for control C 
def signal_handler(signal, frame): 
    print "Control+C has been pressed" 
    #setting the exit flag so that all the threads can get notified 
    exitFlag = 1 
    #wait till all the threads have finished processing and can gracefully exit 
    #I maintain an array for each thread to set the corresponding index when 
    #it has finished its processing. I and all the elements to see if its 0 
    #and based on which I will exit or wait 
    while 1: 
     num = 0 
     for ele in exitList: 
      num &= ele 
     if ele == 0: 
      sys.exit(0) 

class myThread (threading.Thread): 
    #have a queue, thread ID and name for every thread. 
    def __init__(self, threadID, name, q): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.q = q 
    def run(self): 
     print "Starting " + self.name 
     process_data(self.name, self.q, self.threadID) 
     print "Exiting " + self.name 

def process_data(threadName, q, threadID): 
    #while exit flag is not set by the main thread keep processing the data 
    #present in the queue. 
    while not exitFlag: 
     queueLock[threadID].acquire() 
     if not workQueue[threadID].empty(): 
      data = q[threadID].get() 
      queueLock[threadID].release() 
      print "%s processing %s" % (threadName, data) 
     else: 
      queueLock[threadID].release() 
     time.sleep(1) 
    exitThread[threadID] = 1 

threadList = ["Thread-1", "Thread-2", "Thread-3"] 
nameList = ["One", "Two", "Three", "Four", "Five"] 
queueLock = [] 
workQueue = [] 
threads = [] 
threadID = 0 
exitList = [] 
size = 3 
request = 0 
signal.signal(signal.SIGINT, signal_handler) 

# Create new threads 
#by default hard coding the number of threads to 3 
for tName in threadList: 
    workQueue.append(Queue.Queue(10)) 
    queueLock.append(threading.Lock()) 
    exitList.append(0) 
    thread = myThread(threadID, tName, workQueue) 
    print "About to kickoff" 
    thread.start() 
    threads.append(thread) 
    threadID += 1 


for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): 
    af, socktype, proto, canonname, sa = res 
    try: 
     socket.setdefaulttimeout(10) 
     s = socket.socket(af, socktype, proto) 
    except socket.error, msg: 
     s = None 
     continue 
    try: 
     s.bind(sa) 
     s.listen(1) 
    except socket.error, msg: 
     s.close() 
     s = None 
     continue 
    break 
if s is None: 
    print 'could not open socket' 
    sys.exit(1) 
while 1: 
    conn, addr = s.accept() 
    print 'Connected by', addr 
    request += 1 
    #round robin scheduling for each thread 
    thread_index = request % size 
    while 1: 
     data = conn.recv(1024) 
     if not data: break 
     # Fill the queue with the request received 
     queueLock[thread_index].acquire() 
     for word in nameList: 
      workQueue[thread_index].put(word) 
     queueLock[thread_index].release() 
     # Wait for queue to empty 
     while not workQueue[thread_index].empty(): 
      pass 
     conn.send(data) 
    conn.close() 



# Notify threads it's time to exit 
exitFlag = 1 
print "setting the exitFlag" 
# Wait for all threads to complete 
for t in threads: 
    t.join() 
print "Exiting Main Thread" 
+0

您是否试过使用多处理(Process)而不是线程?您可能无法执行多个接受调用,即使它在不同的线程上。使用多处理模块,您可以拥有三个独立执行环境的进程 –

回答

1

有去的​​一对夫妇的事情。

  1. signal_handler(signal, frame):没有设置全局exitFlag。您需要将global exitFlag添加到该函数的顶部。

  2. sys.exit()不会真的退出 - 它只会引发KeyboardInterrupt错误。

  3. socket.error: [Errno 4] Interrupted system call是好东西,它让你的程序不会陷入conn, addr = s.accept()。您应该捕获socket.error例外并使用它们跳出while循环。

+0

感谢您的全球技巧!完全错过了那一部分!而不是使用sys.exit()我用SystemExit(0)似乎工作正常。感谢您的提示。 –