Last active
November 14, 2021 19:24
-
-
Save elibroftw/53f63b1e9c992cc0d8ad1211ee941931 to your computer and use it in GitHub Desktop.
Interprocess Communication in Python using multiprocessing.connection's Listener, Client
This file contains hidden or 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
""" | |
No Copyright by the way. | |
Usage: | |
0. Have two terminals | |
1. In the first terminal run `test.py` | |
2. In the second terminal run `test.py a` | |
3. Magic! If you don't see anything, the arbitrary port 12025 may be in use so use another port | |
Practical applications (requires more than just copy pasting this code): | |
- Simple message queueing: | |
- Start a Listener with multiprocess.Process and give it long tasks (e.g. sending an email, generating graphics) | |
- Communicating with an already running instance of the app without needing a micro web framework | |
I use 127.0.0.1 instead of localhost because resolving the localhost is a performance hinderence. | |
You can use ::1 or whatever the IPv6 version is if you want to use IPv6. | |
""" | |
from multiprocessing.connection import Listener, Client | |
import sys | |
HOST = '127.0.0.1' | |
PORT = 12025 | |
if len(sys.argv) == 1: | |
print('Listener Mode') | |
listener = Listener((HOST, PORT), authkey=b'abc') | |
while True: | |
# Each new Client() requires an acception from the listener | |
# Lisnenr can only interact with one client at a time | |
# since web app has multiple workers, easier to have multiple clients when needed | |
with listener.accept() as conn: | |
a = conn.recv() | |
b = conn.send('from listener') | |
if a == 'exit': | |
print('exit got') | |
sys.exit() | |
# use client | |
else: | |
print('Client Mode') | |
with Client((HOST, PORT), authkey=b'abc') as c: | |
c.send('test') | |
print('sent!') | |
print(c.recv()) | |
with Client((HOST, PORT), authkey=b'abc') as c: | |
c.send('exit') | |
print('sent v2!') | |
print(c.recv()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment