def sock_accept_async(sock): f = CallableContext.get_current().get_future_for(SelectRead(sock)) if f: return (yield f).accept() else: # This is the fallback option for when the current context does not support `SelectRead` # objects. It should probably use a CPU thread rather than blocking the caller. return sock.accept()
def sock_write_async(sock, data): data = data.encode() while data: f = CallableContext.get_current().get_future_for(SelectWrite(sock)) if f: n = (yield f).send(data) else: # This is the fallback option for when the current context does not support `SelectWrite` # objects. It should probably use a CPU thread rather than blocking the caller. n = sock.send(data) data = data[n:]
def sock_readline_async(sock): buf = b"" while buf[-1:] != b"\n": f = CallableContext.get_current().get_future_for(SelectRead(sock)) if f: data = (yield f).recv(1024) # Uncomment this exception to see how with_options(f, always_raise=True) handles # errors in async functions that are not waited upon. #raise Exception("EPIC FAIL!") else: # This is the fallback option for when the current context does not support `SelectRead` # objects. It should probably use a CPU thread rather than blocking the caller. data = sock.recv(1024) if not data: break buf += data if not buf: sock.close() return buf.decode(errors='ignore')