如何“断开”插座以允许重新连接

时间:2015-07-17 14:01:24

标签: python-2.7 sockets

我正在创建一个分布式计算服务器和相应的客户端Python。我的服务器使用多处理创建一个守护进程池,它等待来自客户端的连接,然后将数据发送到客户端。然后,客户端执行最多可能需要12个小时的操作。完成后,客户端将重新发送数据。

因为操作可能需要很长时间,所以我希望能够断开服务器和客户端套接字,以便该过程可以为更多客户端提供服务。但是,我只找到了关闭套接字的函数。

SERVER

111 def Worker(socket, lock):
122         "Workers follow this workflow"
123         while True:
124                 #Accept connection
125                 client, address = socket.accept()
126                 logger.debug("{u} connected".format(u=address))
127                 client.send("Connection established")
128                 #Receive orders
129                 orders = client.recv(1024)
130                 if orders == "REQUEST WORK":
131                         lock.acquire()
132                         jobname = findjob()
133                         lock.release()
134                         print jobname
135                         filenames = getjobfiles(jobname)
136                         #Send job description
137                         numfiles = len(filenames)
138                         desc = "SENDING " + jobname + " " + str(numfiles) + " "
139                         blank =''
140                         jobdesc = desc.replace('\n', blank)
141                         for i in filenames:
142                                 name = i.replace(Jobsdir, blank)
143                                 jobdesc += name
144                                 jobdesc += ' '
145                         client.send(jobdesc)
146                         ready = client.recv(1024)
147                         while ready != "READY":
148                                 ready = client.recv(1024)
149                         for i in filenames:
150                                 sendfile(client, i)
151                 elif orders == 'RETURN DATA':
152                         client.send('READY')
153                         #Receive information about files to be sent
154                         data = client.recv(1024)
155                         #Interpret data
156                         jobname, filenames = parse(data)
157                         numfiles = len(filenames)
158                         client.send('READY')
159                         #Receive all of the files from client
160                         for i in range(numfiles):
161                                 print 'RECEIVING ', filenames[i]
162                                 recvfile(client, jobname, filenames[i])
163                         #Lock and remove job from jobs list
164                         lock.acquire()
165                         removejob(jobname)
166                         lock.release()
167                         empty = jobsempty()
168                         if empty:
169                                 os.system('python Jobs/analyze.py')
170                                 print 'JOBS AND ANALYSIS FINISHED'
171                                 sys.exit(0)




 CLIENT

 80 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 81 #Get local machine name
 82 host = socket.gethostname()
 83 port = 5007
 84 while True:
 85         #Connect to server
 86         s.connect((host, port))
 87         #Receive confirmation of connection
 88         data = s.recv(1024)
 89         #Request work
 90         s.send('REQUEST WORK')
 91         #Receive string containing file names
 92         data = s.recv(1024)
 93         if data == 'NO NEW WORK': break
 94         #Get all files
 95         jobname, filenames = parse(data)
 96         os.mkdir(jobname)
 97         numfiles = len(filenames)
 98         print numfiles
 99         s.send('READY')
100         for i in range(numfiles):
101                 print i
102                 print 'RECEIVING ', filenames[i]
103                 recvfile(s, filenames[i])
104         print 'ALL FILES RECEIVED. CLOSING CONNECTION'
105         #Close socket
106         s.shutdown()
107         #Do work
108         os.system('python %s/orders.py'%jobname)
109         #After completing work, initiate another connection to send back files
110         time.sleep(4)
111         s.connect((host, port))
112         #Receive confirmation of connection
113         data = s.recv(1024)
114         #Tell server to prepare for incoming data
115         s.send('RETURN DATA')
116         data = s.recv(1024)
117         while data != 'READY':
118                 data = s.recv(1024)
119         #Tell server what is being sent back
120         newfilenames = getjobfiles(jobname)
121         newnumfiles = len(newfilenames)
122         desc = "SENDING " + jobname + " " + str(numfiles) + " "
123         blank = ''
124         jobdesc = desc.replace('\n', blank)
125         for i in filenames:
126                 jobdesc += name
127                 jobdesc += ' '
128         s.send(jobdesc)
129         #Wait for server to be ready
130         ready = s.recv(1024)
131         while ready != 'READY':
132                 ready = s.recv(1024)
133         #Send files
134         for i in filenames:
135                 sendfile(s, i)
136         s.recv(1024)
137         print 'ALL FILES SENT'

这样做有好办法吗?我敢肯定必须有办法。

感谢您的帮助

0 个答案:

没有答案