[問題] 如何讓程式等待

看板Python作者 (沒有名字的怪物)時間12年前 (2013/03/22 17:04), 編輯推噓3(305)
留言8則, 4人參與, 最新討論串1/1
請教一下,我想寫個Server用執行緒去處理連線 那麼主程序除了用raw_input() 讓它一直等在那之外,有其它方式嗎?? 謝謝 import socket,hashlib import thread, time, threading class ServerThread(threading.Thread): def __init__(self,lock,threadName,ServerSocket): super(ServerThread,self).__init__(name=threadName) self.lock=lock self.Socket=ServerSocket def run(self): global count shaAnswer="" self.lock.acquire() print "Thread" while 1: client,address=self.Socket.accept() data=client.recv(512) data=data.strip('\x00') ShaResult=hashlib.sha1(data).hexdigest() client.send(ShaResult) client.close() self.lock.release() if __name__ == '__main__': count=0 lock=thread.allocate_lock() host = '' port = 7779 backlog = 5 size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) print "listen" s.listen(backlog) print "go to loop" ServerThread(lock,"ServerThread-",s,).start() raw_input() s.close() -- 生命是個過程 可悲的是它不能重來 可喜的是它也不需要重來 ~~~童夢奇緣~~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.127.191.91

03/22 17:17, , 1F
time.sleep(100) 可以讓程式停在這邊100秒
03/22 17:17, 1F

03/22 18:33, , 2F
無限迴圈可以嗎?
03/22 18:33, 2F

03/22 19:05, , 3F
嗯,這方法也想過,看來只能這樣,謝謝
03/22 19:05, 3F

03/22 22:04, , 4F
t = ServerThread(lock,"ServerThread-",s)
03/22 22:04, 4F

03/22 22:04, , 5F
t.start()
03/22 22:04, 5F

03/22 22:05, , 6F
t.join()
03/22 22:05, 6F

03/22 22:06, , 7F
這樣不行嗎@@
03/22 22:06, 7F

03/22 22:52, , 8F
原來如此,謝謝你
03/22 22:52, 8F
文章代碼(AID): #1HJ1wxow (Python)
文章代碼(AID): #1HJ1wxow (Python)