Skip to content

Instantly share code, notes, and snippets.

@fredokun
Created August 30, 2017 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredokun/4125bc13396e595ff0f8a4b4eae09980 to your computer and use it in GitHub Desktop.
Save fredokun/4125bc13396e595ff0f8a4b4eae09980 to your computer and use it in GitHub Desktop.
Python 3 Client/server using multiprocessing with asynchronous pipes
import multiprocessing as mp
import time
import MPServer
if __name__ == "__main__":
#mp.set_start_method('fork')
print("[Client] start")
request, reply = mp.Pipe()
serv = mp.Process(target=MPServer.run_server, args=(reply,))
serv.start()
print("[Client] launched server")
#time.sleep(3)
print("[Client] send 'test'")
request.send("test")
print("[Client] wait loop")
avail = False
while not avail:
print("[Client] wait 1 second")
avail = request.poll(1)
msg = request.recv()
print("[Client] receives '{}'".format(msg))
print("[Client] terminates server")
serv.terminate()
serv.join()
print("[Client] stop")
import multiprocessing as mp
import tkinter as tk
def run_server(reply):
print("[Server] started")
req = reply.recv()
print("[Server] received '{}'".format(req))
window = tk.Tk()
button = tk.Button(window, text=str(req), command=quit_server(reply))
button.pack()
window.mainloop()
def quit_server(reply):
def callback():
print("[Server] send 'quit'")
reply.send('quit')
print("[Server] wait for termination")
return callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment