I'm trying to utilise the Client/Server architecture of the multiprocessing
module to facilitate communication between my Python scripts. I want to be able to implement a separate connection listener apart from my main event loop, using asyncore
. How can I do this?
While there is no outward documentation of how to do so, I've tried to construct a basic asyncore.dispatcher
using the listener socket from multiprocessing.Listener
:
import asyncore
import socket
from multiprocessing.connection import Listener, Client
class ConnectionListener(asyncore.dispatcher):
def __init__(self, ln):
asyncore.dispatcher.__init__(self)
self.set_socket(ln._listener._socket)
def handle_accepted(self, sock, addr):
print("Accepted a client!")
if __name__ == "__main__":
address = ('localhost', 19378)
try:
ln = Listener(address)
x = ConnectionListener(ln)
while True:
asyncore.loop()
except socket.error:
c = Client(address)
print("Client is trying to connect")
While the server does loop, it never fires handle_accepted
.
我认为您正在寻找的方法是handle_accept,而不是handle_accepted。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.