def __init__(self, lsock): # Create apoll object apoll = pyasynchio.apoll() self.lsock = lsock self.apoll = apoll # Initiate asynchronous accept on the listening socket apoll.accept(lsock)
def test_it_with_asynch_client(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ap = pyasynchio.apoll() ap.connect(sock, ("localhost", self.port)) pr = ap.poll() self.assert_(pr[0][0].name == "connect" and pr[0][0].success) data = "Statically typed bigot" ap.send(sock, data) pr = ap.poll() self.assert_(pr[0][0].name == "send" and pr[0][0].success) ap.recv(sock, len(data)) pr = ap.poll() self.assert_(pr[0][0].name == "recv" and pr[0][0].success and pr[0][1].data == data)
import sys import socket import pyasynchio # Create and bind socket lsock = socket.socket() lsock.bind((sys.argv[1], int(sys.argv[2]))) lsock.listen(5) # Create apoll object apoll = pyasynchio.apoll() # Issue first asynch accept through apoll object apoll.accept(lsock) while True: # poll results from apoll object events = apoll.poll() # process them one by one for g, s in events: # if operation was accept if g.name == 'accept': # moreover, if it is successful if g.success: # issue asynchronous read through apoll object apoll.recv(s.asock, 1024) # issue another asynch accept, so that more clients can connect apoll.accept(s.lsock) # if operation was recv elif g.name == 'recv': # if received some data and connection isnt broken (just as recv it presents empty string if client disconnected) if g.success and s.data != '':
import pyasynchio import socket lsock = socket.socket() lsock.bind(('', 3811)) lsock.listen(5); ap = pyasynchio.apoll(); class echo_handler: def __init__(self, sock): self.genobj = self(sock) def __call__(self, sock): ap.recv(sock, 1024, act = self); yield None while True: if self.spec.data == "": return; ap.send(sock, self.spec.data, act = self); yield None ap.recv(sock, 1024, act = self); yield None def next(self): self.genobj.next() def notify(self, gen, spec): self.gen = gen self.spec = spec class accept_handler:
def __init__(self, sock): apoll = pyasynchio.apoll() self.apoll = apoll self.sock = sock # initiate asynchronous dgram recv apoll.recvfrom(sock, 65536)