Browse Source

Join forked processes when they finish.

When a forked process finishes, the main process will join it as soon as it can (the next time 'accept()' is called). This reduces the number of zombie processes.
Steven Engler 5 years ago
parent
commit
c6d5b652e5
1 changed files with 14 additions and 0 deletions
  1. 14 0
      src/bandwidth_tester.py

+ 14 - 0
src/bandwidth_tester.py

@@ -141,6 +141,8 @@ if __name__ == '__main__':
 		#
 		endpoint = ('127.0.0.1', 4747)
 		processes = []
+		processes_map = {}
+		joinable_connections = multiprocessing.Queue()
 		conn_counter = [0]
 		group_queue = multiprocessing.Queue()
 		bw_queue = multiprocessing.Queue()
@@ -160,6 +162,8 @@ if __name__ == '__main__':
 				server.run()
 			except KeyboardInterrupt:
 				socket.close()
+			finally:
+				joinable_connections.put(conn_id)
 			#
 		#
 		def accept_callback(socket):
@@ -168,6 +172,7 @@ if __name__ == '__main__':
 			#logging.debug('Adding connection %d', conn_id)
 			p = multiprocessing.Process(target=start_server_conn, args=(socket, conn_id))
 			processes.append(p)
+			processes_map[conn_id] = p
 			p.start()
 		#
 		l = basic_protocols.ServerListener(endpoint, accept_callback)
@@ -175,6 +180,15 @@ if __name__ == '__main__':
 		try:
 			while True:
 				l.accept()
+				try:
+					while True:
+						conn_id = joinable_connections.get(False)
+						p = processes_map[conn_id]
+						p.join()
+					#
+				except queue.Empty:
+					pass
+				#
 			#
 		except KeyboardInterrupt:
 			print()