Python源码示例:asyncio.streams()
示例1
def start(self):
await self.all_done.acquire()
await self.set_status('Working', progress=0)
socket_name = '{}.sock'.format(self.task_id)
self.socket_path = os.path.join(os.getcwd(), socket_name)
if os.path.exists(self.socket_path):
os.remove(self.socket_path)
server = await asyncio.streams.start_unix_server(self.client_connected_cb, path=self.socket_path, loop=self.loop)
try:
self.dirsearch_proc = self.loop.run_in_executor(
ProcessPoolExecutor(), start_program, self.target,
self.task_id, self.project_uuid, self.socket_path,
self.params_object
)
except Exception as exc:
print("Exception starting ProcessPoolExecutor", exc)
示例2
def start(self):
await self.all_done.acquire()
host, port = self.target.split(':')
args = self.params['program'][0] + " host={} port={}".format(host, port)
await self.set_status('Working', progress=0)
socket_name = '{}.sock'.format(self.task_id)
self.socket_path = os.path.join(os.getcwd(), socket_name)
if os.path.exists(self.socket_path):
os.remove(self.socket_path)
await asyncio.streams.start_unix_server(self.client_connected_cb, path=self.socket_path, loop=self.loop)
try:
self.patator_proc = self.loop.run_in_executor(
ProcessPoolExecutor(), start_program, args,
host, port, self.task_id, self.project_uuid,
self.socket_path
)
except Exception as exc:
print("Exception starting ProcessPoolExecutor", exc)
示例3
def __init__(self, *, loop=None, **kwargs):
asyncio.streams.FlowControlMixin.__init__(self, loop=loop)
DataQueue.__init__(self, loop=loop)
self.paused = False
self.transport = None
self.writer = None
self._should_close = False
self._message = None
self._payload = None
self._payload_parser = None
self._reading_paused = False
self._timer = None
self._skip_status = ()
self._tail = b''
self._upgraded = False
self._parser = None
示例4
def __init__(self, *, loop=None, **kwargs):
asyncio.streams.FlowControlMixin.__init__(self, loop=loop)
DataQueue.__init__(self, loop=loop)
self.paused = False
self.transport = None
self.writer = None
self._should_close = False
self._message = None
self._payload = None
self._payload_parser = None
self._reading_paused = False
self._timer = None
self._skip_status = ()
self._tail = b''
self._upgraded = False
self._parser = None
示例5
def start(self, loop):
"""
Starts the TCP server, so that it listens on port 1234.
For each client that connects, the accept_client method gets
called. This method runs the loop until the server sockets
are ready to accept connections.
"""
self.server = loop.run_until_complete(
asyncio.streams.start_server(self._accept_client,
'127.0.0.1', 12345,
loop=loop))
示例6
def start(self, loop):
"""
Starts the TCP server, so that it listens on port 12345.
For each client that connects, the accept_client method gets
called. This method runs the loop until the server sockets
are ready to accept connections.
"""
self.server = loop.run_until_complete(
asyncio.streams.start_server(self._accept_client,
'127.0.0.1', 12345,
loop=loop))
示例7
def main():
loop = asyncio.get_event_loop()
# creates a server and starts listening to TCP connections
server = MyServer()
server.start(loop)
@asyncio.coroutine
def client():
reader, writer = yield from asyncio.streams.open_connection(
'127.0.0.1', 12345, loop=loop)
def send(msg):
print("> " + msg)
writer.write((msg + '\n').encode("utf-8"))
def recv():
msgback = (yield from reader.readline()).decode("utf-8").rstrip()
print("< " + msgback)
return msgback
# send a line
send("add 1 2")
msg = yield from recv()
Ns = list(range(100, 100000, 10000))
times = []
for N in Ns:
t0 = time.time()
send("repeat {} hello world ".format(N))
msg = yield from recv()
assert msg == 'begin'
while True:
msg = (yield from reader.readline()).decode("utf-8").rstrip()
if msg == 'end':
break
t1 = time.time()
dt = t1 - t0
print("Time taken: {:.3f} seconds ({:.6f} per repetition)"
.format(dt, dt/N))
times.append(dt)
writer.close()
yield from asyncio.sleep(0.5)
# creates a client and connects to our server
try:
loop.run_until_complete(client())
server.stop(loop)
finally:
loop.close()
示例8
def main():
loop = asyncio.get_event_loop()
# creates a server and starts listening to TCP connections
server = MyServer()
server.start(loop)
@asyncio.coroutine
def client():
reader, writer = yield from asyncio.streams.open_connection(
'127.0.0.1', 12345, loop=loop)
def send(msg):
print("> " + msg)
writer.write((msg + '\n').encode("utf-8"))
def recv():
msgback = (yield from reader.readline()).decode("utf-8").rstrip()
print("< " + msgback)
return msgback
# send a line
send("add 1 2")
msg = yield from recv()
send("repeat 5 hello")
msg = yield from recv()
assert msg == 'begin'
while True:
msg = yield from recv()
if msg == 'end':
break
writer.close()
yield from asyncio.sleep(0.5)
# creates a client and connects to our server
try:
loop.run_until_complete(client())
server.stop(loop)
finally:
loop.close()