ソケット通信は異なるサーバ間同士でのやりとりかと思っていたが、別々のスレッド同士でもできるのね。
import socket import os.path import time from threading import Thread, current_thread SOCK_FILE = "./mailbox" BUFFER_SIZE = 1024 class Sender(Thread): def run(self) -> None: self.name = "Sender" client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) client.connect(SOCK_FILE) messages = ["Hello", " ", "world!"] for msg in messages: print(f"{current_thread().name}: Send: '{msg}'") client.sendall(str.encode(msg)) client.close() class Receiver(Thread): def run(self) -> None: self.name = "Receiver" server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server.bind(SOCK_FILE) server.listen() print( f"{current_thread().name}: Listening for incoming messages...") conn, addr = server.accept() while True: data = conn.recv(BUFFER_SIZE) if not data: break message = data.decode() print(f"{current_thread().name}: Received: '{message}'") server.close() def main() -> None: if os.path.exists(SOCK_FILE): os.remove(SOCK_FILE) receiver = Receiver() receiver.start() time.sleep(1) sender = Sender() sender.start() for thread in [receiver, sender]: thread.join() os.remove(SOCK_FILE) if __name__ == "__main__": main()
$ python3 sockets.py
Receiver: Listening for incoming messages…
Sender: Send: ‘Hello’
Receiver: Received: ‘Hello’
Sender: Send: ‘ ‘
Receiver: Received: ‘ ‘
Sender: Send: ‘world!’
Receiver: Received: ‘world!’