Created
August 30, 2017 11:42
-
-
Save fredokun/4125bc13396e595ff0f8a4b4eae09980 to your computer and use it in GitHub Desktop.
Python 3 Client/server using multiprocessing with asynchronous pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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