From 5e4efd7d73c74b39a79fb7ac2dd70f88ccbc866a Mon Sep 17 00:00:00 2001 From: Denis Afonso Date: Wed, 16 Nov 2016 22:49:45 +0000 Subject: [PATCH] Some minor improvements on coding style. Fixes issue #464 --- examples/cacheclt.py | 11 ++++---- examples/cachesvr.py | 4 ++- examples/child_process.py | 6 +++- examples/crawl.py | 15 +++++----- examples/echo_client_tulip.py | 3 ++ examples/echo_server_tulip.py | 3 ++ examples/fetch0.py | 8 +++--- examples/fetch1.py | 21 ++++++++------ examples/fetch2.py | 35 ++++++++++++++---------- examples/fetch3.py | 28 +++++++++---------- examples/fuzz_as_completed.py | 26 ++++++++++-------- examples/qspeed.py | 9 +++++- examples/shell.py | 8 +++++- examples/simple_tcp_server.py | 9 +++--- examples/sink.py | 11 ++++---- examples/source.py | 18 ++++++------ examples/source1.py | 15 +++++----- examples/stacks.py | 24 ++++++++-------- examples/subprocess_attach_read_pipe.py | 7 ++++- examples/subprocess_attach_write_pipe.py | 10 +++++-- examples/subprocess_shell.py | 8 ++++-- examples/tcp_echo.py | 6 +++- examples/timing_tcp_server.py | 6 ++-- examples/udp_echo.py | 4 +++ 24 files changed, 180 insertions(+), 115 deletions(-) diff --git a/examples/cacheclt.py b/examples/cacheclt.py index b11a4d1a..c7c75bc5 100644 --- a/examples/cacheclt.py +++ b/examples/cacheclt.py @@ -4,11 +4,12 @@ """ import argparse -import asyncio -from asyncio import test_utils import json import logging +import asyncio + + ARGS = argparse.ArgumentParser(description='Cache client example.') ARGS.add_argument( '--tls', action='store_true', dest='tls', @@ -106,7 +107,7 @@ def activity(self): self.reader, self.writer = yield from asyncio.open_connection( self.host, self.port, ssl=self.sslctx, loop=self.loop) except Exception as exc: - backoff = min(args.max_backoff, backoff + (backoff//2) + 1) + backoff = min(args.max_backoff, backoff + (backoff // 2) + 1) logging.info('Error connecting: %r; sleep %s', exc, backoff) yield from asyncio.sleep(backoff, loop=self.loop) continue @@ -172,7 +173,7 @@ def main(): loop = asyncio.new_event_loop() sslctx = None if args.tls: - sslctx = test_utils.dummy_ssl_context() + sslctx = asyncio.test_utils.dummy_ssl_context() cache = CacheClient(args.host, args.port, sslctx=sslctx, loop=loop) try: loop.run_until_complete( @@ -191,7 +192,7 @@ def w(g): key = 'foo-%s' % label while True: - logging.info('%s %s', label, '-'*20) + logging.info('%s %s', label, '-' * 20) try: ret = yield from w(cache.set(key, 'hello-%s-world' % label)) logging.info('%s set %s', label, ret) diff --git a/examples/cachesvr.py b/examples/cachesvr.py index 053f9c21..2f50a77a 100644 --- a/examples/cachesvr.py +++ b/examples/cachesvr.py @@ -57,12 +57,14 @@ """ import argparse -import asyncio import json import logging import os import random +import asyncio + + ARGS = argparse.ArgumentParser(description='Cache server example.') ARGS.add_argument( '--tls', action='store_true', dest='tls', diff --git a/examples/child_process.py b/examples/child_process.py index 3fac175e..a03fce81 100644 --- a/examples/child_process.py +++ b/examples/child_process.py @@ -26,6 +26,7 @@ # Return a write-only transport wrapping a writable pipe # + @asyncio.coroutine def connect_write_pipe(file): loop = asyncio.get_event_loop() @@ -36,10 +37,12 @@ def connect_write_pipe(file): # Wrap a readable pipe in a stream # + @asyncio.coroutine def connect_read_pipe(file): loop = asyncio.get_event_loop() stream_reader = asyncio.StreamReader(loop=loop) + def factory(): return asyncio.StreamReaderProtocol(stream_reader) transport, _ = yield from loop.connect_read_pipe(factory, file) @@ -85,7 +88,7 @@ def writeall(fd, buf): stderr, stderr_transport = yield from connect_read_pipe(p.stderr) # interact with subprocess - name = {stdout:'OUT', stderr:'ERR'} + name = {stdout: 'OUT', stderr: 'ERR'} registered = {asyncio.Task(stderr.readline()): stderr, asyncio.Task(stdout.readline()): stdout} while registered: @@ -116,6 +119,7 @@ def writeall(fd, buf): stdout_transport.close() stderr_transport.close() + if __name__ == '__main__': if sys.platform == 'win32': loop = ProactorEventLoop() diff --git a/examples/crawl.py b/examples/crawl.py index 4cb76d26..c081a231 100644 --- a/examples/crawl.py +++ b/examples/crawl.py @@ -15,15 +15,16 @@ # - Handle out of file descriptors directly? (How?) import argparse -import asyncio -import asyncio.locks -import cgi -from http.client import BadStatusLine import logging import re import sys import time import urllib.parse +import cgi +from http.client import BadStatusLine + +import asyncio +import asyncio.locks ARGS = argparse.ArgumentParser(description="Web crawler") @@ -341,7 +342,7 @@ def send_request(self): self.headers.append(('User-Agent', 'asyncio-example-crawl/0.0')) self.headers.append(('Host', self.netloc)) self.headers.append(('Accept', '*/*')) - ##self.headers.append(('Accept-Encoding', 'gzip')) + # self.headers.append(('Accept-Encoding', 'gzip')) for key, value in self.headers: line = '%s: %s' % (key, value) yield from self.putline(line) @@ -519,7 +520,7 @@ def fetch(self): self.exceptions.append(exc) self.log(1, 'try', self.tries, 'for', self.url, 'raised', repr(exc)) - ##import pdb; pdb.set_trace() + # import pdb; pdb.set_trace() # Don't reuse the connection in this case. finally: if self.request is not None: @@ -534,7 +535,7 @@ def fetch(self): self.next_url = urllib.parse.urljoin(self.url, next_url) if self.max_redirect > 0: self.log(1, 'redirect to', self.next_url, 'from', self.url) - self.crawler.add_url(self.next_url, self.max_redirect-1) + self.crawler.add_url(self.next_url, self.max_redirect - 1) else: self.log(0, 'redirect limit reached for', self.next_url, 'from', self.url) diff --git a/examples/echo_client_tulip.py b/examples/echo_client_tulip.py index 88124efe..0bd10335 100644 --- a/examples/echo_client_tulip.py +++ b/examples/echo_client_tulip.py @@ -1,7 +1,9 @@ import asyncio + END = b'Bye-bye!\n' + @asyncio.coroutine def echo_client(): reader, writer = yield from asyncio.open_connection('localhost', 8000) @@ -15,6 +17,7 @@ def echo_client(): break writer.close() + loop = asyncio.get_event_loop() loop.run_until_complete(echo_client()) loop.close() diff --git a/examples/echo_server_tulip.py b/examples/echo_server_tulip.py index 8167e540..8b98e9a8 100644 --- a/examples/echo_server_tulip.py +++ b/examples/echo_server_tulip.py @@ -1,9 +1,11 @@ import asyncio + @asyncio.coroutine def echo_server(): yield from asyncio.start_server(handle_connection, 'localhost', 8000) + @asyncio.coroutine def handle_connection(reader, writer): while True: @@ -12,6 +14,7 @@ def handle_connection(reader, writer): break writer.write(data) + loop = asyncio.get_event_loop() loop.run_until_complete(echo_server()) try: diff --git a/examples/fetch0.py b/examples/fetch0.py index 2712abdd..c31a3571 100644 --- a/examples/fetch0.py +++ b/examples/fetch0.py @@ -2,12 +2,12 @@ import sys -from asyncio import get_event_loop, open_connection, coroutine +import asyncio -@coroutine +@asyncio.coroutine def fetch(): - r, w = yield from open_connection('python.org', 80) + r, w = yield from asyncio.open_connection('python.org', 80) request = 'GET / HTTP/1.0\r\n\r\n' print('>', request, file=sys.stderr) w.write(request.encode('latin-1')) @@ -23,7 +23,7 @@ def fetch(): def main(): - loop = get_event_loop() + loop = asyncio.get_event_loop() try: body = loop.run_until_complete(fetch()) finally: diff --git a/examples/fetch1.py b/examples/fetch1.py index 7f5d8873..190c9565 100644 --- a/examples/fetch1.py +++ b/examples/fetch1.py @@ -6,7 +6,7 @@ import sys import urllib.parse -from asyncio import get_event_loop, open_connection, coroutine +import asyncio class Response: @@ -18,27 +18,30 @@ def __init__(self, verbose=True): self.reason = None # 'Ok' self.headers = [] # [('Content-Type', 'text/html')] - @coroutine + @asyncio.coroutine def read(self, reader): - @coroutine + @asyncio.coroutine def getline(): return (yield from reader.readline()).decode('latin-1').rstrip() status_line = yield from getline() - if self.verbose: print('<', status_line, file=sys.stderr) + if self.verbose: + print('<', status_line, file=sys.stderr) self.http_version, status, self.reason = status_line.split(None, 2) self.status = int(status) while True: header_line = yield from getline() if not header_line: break - if self.verbose: print('<', header_line, file=sys.stderr) + if self.verbose: + print('<', header_line, file=sys.stderr) # TODO: Continuation lines. key, value = header_line.split(':', 1) self.headers.append((key, value.strip())) - if self.verbose: print(file=sys.stderr) + if self.verbose: + print(file=sys.stderr) -@coroutine +@asyncio.coroutine def fetch(url, verbose=True): parts = urllib.parse.urlparse(url) if parts.scheme == 'http': @@ -57,7 +60,7 @@ def fetch(url, verbose=True): request = 'GET %s HTTP/1.0\r\n\r\n' % path if verbose: print('>', request, file=sys.stderr, end='') - r, w = yield from open_connection(parts.hostname, port, ssl=ssl) + r, w = yield from asyncio.open_connection(parts.hostname, port, ssl=ssl) w.write(request.encode('latin-1')) response = Response(verbose) yield from response.read(r) @@ -66,7 +69,7 @@ def fetch(url, verbose=True): def main(): - loop = get_event_loop() + loop = asyncio.get_event_loop() try: body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv)) finally: diff --git a/examples/fetch2.py b/examples/fetch2.py index a75f7ba7..3ca9d1f1 100644 --- a/examples/fetch2.py +++ b/examples/fetch2.py @@ -7,7 +7,7 @@ import urllib.parse from http.client import BadStatusLine -from asyncio import get_event_loop, open_connection, coroutine +import asyncio class Request: @@ -34,13 +34,13 @@ def __init__(self, url, verbose=True): self.reader = None self.writer = None - @coroutine + @asyncio.coroutine def connect(self): if self.verbose: print('* Connecting to %s:%s using %s' % (self.hostname, self.port, 'ssl' if self.ssl else 'tcp'), file=sys.stderr) - self.reader, self.writer = yield from open_connection(self.hostname, + self.reader, self.writer = yield from asyncio.open_connection(self.hostname, self.port, ssl=self.ssl) if self.verbose: @@ -51,20 +51,22 @@ def connect(self): def putline(self, line): self.writer.write(line.encode('latin-1') + b'\r\n') - @coroutine + @asyncio.coroutine def send_request(self): request = '%s %s %s' % (self.method, self.full_path, self.http_version) - if self.verbose: print('>', request, file=sys.stderr) + if self.verbose: + print('>', request, file=sys.stderr) self.putline(request) if 'host' not in {key.lower() for key, _ in self.headers}: self.headers.insert(0, ('Host', self.netloc)) for key, value in self.headers: line = '%s: %s' % (key, value) - if self.verbose: print('>', line, file=sys.stderr) + if self.verbose: + print('>', line, file=sys.stderr) self.putline(line) self.putline('') - @coroutine + @asyncio.coroutine def get_response(self): response = Response(self.reader, self.verbose) yield from response.read_headers() @@ -81,14 +83,15 @@ def __init__(self, reader, verbose=True): self.reason = None # 'Ok' self.headers = [] # [('Content-Type', 'text/html')] - @coroutine + @asyncio.coroutine def getline(self): return (yield from self.reader.readline()).decode('latin-1').rstrip() - @coroutine + @asyncio.coroutine def read_headers(self): status_line = yield from self.getline() - if self.verbose: print('<', status_line, file=sys.stderr) + if self.verbose: + print('<', status_line, file=sys.stderr) status_parts = status_line.split(None, 2) if len(status_parts) != 3: raise BadStatusLine(status_line) @@ -98,13 +101,15 @@ def read_headers(self): header_line = yield from self.getline() if not header_line: break - if self.verbose: print('<', header_line, file=sys.stderr) + if self.verbose: + print('<', header_line, file=sys.stderr) # TODO: Continuation lines. key, value = header_line.split(':', 1) self.headers.append((key, value.strip())) - if self.verbose: print(file=sys.stderr) + if self.verbose: + print(file=sys.stderr) - @coroutine + @asyncio.coroutine def read(self): nbytes = None for key, value in self.headers: @@ -118,7 +123,7 @@ def read(self): return body -@coroutine +@asyncio.coroutine def fetch(url, verbose=True): request = Request(url, verbose) yield from request.connect() @@ -129,7 +134,7 @@ def fetch(url, verbose=True): def main(): - loop = get_event_loop() + loop = asyncio.get_event_loop() try: body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv)) finally: diff --git a/examples/fetch3.py b/examples/fetch3.py index 00cc33a5..9771eb2d 100644 --- a/examples/fetch3.py +++ b/examples/fetch3.py @@ -8,7 +8,7 @@ import urllib.parse from http.client import BadStatusLine -from asyncio import get_event_loop, open_connection, coroutine +import asyncio class ConnectionPool: @@ -22,10 +22,10 @@ def close(self): for _, writer in self.connections.values(): writer.close() - @coroutine + @asyncio.coroutine def open_connection(self, host, port, ssl): port = port or (443 if ssl else 80) - ipaddrs = yield from get_event_loop().getaddrinfo(host, port) + ipaddrs = yield from asyncio.get_event_loop().getaddrinfo(host, port) if self.verbose: print('* %s resolves to %s' % (host, ', '.join(ip[4][0] for ip in ipaddrs)), @@ -41,7 +41,7 @@ def open_connection(self, host, port, ssl): if self.verbose: print('* Reusing pooled connection', key, file=sys.stderr) return conn - reader, writer = yield from open_connection(host, port, ssl=ssl) + reader, writer = yield from asyncio.open_connection(host, port, ssl=ssl) host, port, *_ = writer.get_extra_info('peername') key = host, port, ssl self.connections[key] = reader, writer @@ -78,7 +78,7 @@ def vprint(self, *args): if self.verbose: print(*args, file=sys.stderr) - @coroutine + @asyncio.coroutine def connect(self, pool): self.vprint('* Connecting to %s:%s using %s' % (self.hostname, self.port, 'ssl' if self.ssl else 'tcp')) @@ -89,13 +89,13 @@ def connect(self, pool): self.vprint('* Connected to %s' % (self.writer.get_extra_info('peername'),)) - @coroutine + @asyncio.coroutine def putline(self, line): self.vprint('>', line) self.writer.write(line.encode('latin-1') + b'\r\n') - ##yield from self.writer.drain() + # yield from self.writer.drain() - @coroutine + @asyncio.coroutine def send_request(self): request = '%s %s %s' % (self.method, self.full_path, self.http_version) yield from self.putline(request) @@ -106,7 +106,7 @@ def send_request(self): yield from self.putline(line) yield from self.putline('') - @coroutine + @asyncio.coroutine def get_response(self): response = Response(self.reader, self.verbose) yield from response.read_headers() @@ -127,13 +127,13 @@ def vprint(self, *args): if self.verbose: print(*args, file=sys.stderr) - @coroutine + @asyncio.coroutine def getline(self): line = (yield from self.reader.readline()).decode('latin-1').rstrip() self.vprint('<', line) return line - @coroutine + @asyncio.coroutine def read_headers(self): status_line = yield from self.getline() status_parts = status_line.split(None, 2) @@ -161,7 +161,7 @@ def get_header(self, key, default=None): return v return default - @coroutine + @asyncio.coroutine def read(self): nbytes = None for key, value in self.headers: @@ -192,7 +192,7 @@ def read(self): return body -@coroutine +@asyncio.coroutine def fetch(url, verbose=True, max_redirect=10): pool = ConnectionPool(verbose) try: @@ -218,7 +218,7 @@ def main(): loop = ProactorEventLoop() set_event_loop(loop) else: - loop = get_event_loop() + loop = asyncio.get_event_loop() try: body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv)) finally: diff --git a/examples/fuzz_as_completed.py b/examples/fuzz_as_completed.py index 123fbf1b..90cffaaa 100644 --- a/examples/fuzz_as_completed.py +++ b/examples/fuzz_as_completed.py @@ -2,18 +2,21 @@ """Fuzz tester for as_completed(), by Glenn Langford.""" -import asyncio import itertools import random import sys +import asyncio + + @asyncio.coroutine def sleeper(time): yield from asyncio.sleep(time) return time + @asyncio.coroutine -def watcher(tasks,delay=False): +def watcher(tasks, delay=False): res = [] for t in asyncio.as_completed(tasks): r = yield from t @@ -22,8 +25,8 @@ def watcher(tasks,delay=False): # simulate processing delay process_time = random.random() / 10 yield from asyncio.sleep(process_time) - #print(res) - #assert(sorted(res) == res) + # print(res) + # assert(sorted(res) == res) if sorted(res) != res: print('FAIL', res) print('------------') @@ -31,25 +34,26 @@ def watcher(tasks,delay=False): print('.', end='') sys.stdout.flush() + loop = asyncio.get_event_loop() print('Pass 1') # All permutations of discrete task running times must be returned # by as_completed in the correct order. -task_times = [0, 0.1, 0.2, 0.3, 0.4 ] # 120 permutations +task_times = [0, 0.1, 0.2, 0.3, 0.4] # 120 permutations for times in itertools.permutations(task_times): - tasks = [ asyncio.Task(sleeper(t)) for t in times ] + tasks = [asyncio.Task(sleeper(t)) for t in times] loop.run_until_complete(asyncio.Task(watcher(tasks))) print() print('Pass 2') # Longer task times, with randomized duplicates. 100 tasks each time. -longer_task_times = [x/10 for x in range(30)] +longer_task_times = [x / 10 for x in range(30)] for i in range(20): task_times = longer_task_times * 10 random.shuffle(task_times) - #print('Times', task_times[:500]) - tasks = [ asyncio.Task(sleeper(t)) for t in task_times[:100] ] + # print('Times', task_times[:500]) + tasks = [asyncio.Task(sleeper(t)) for t in task_times[:100]] loop.run_until_complete(asyncio.Task(watcher(tasks))) print() @@ -61,8 +65,8 @@ def watcher(tasks,delay=False): for i in range(20): task_times = longer_task_times * 10 random.shuffle(task_times) - #print('Times', task_times[:200]) - tasks = [ asyncio.Task(sleeper(t)) for t in task_times[:200] ] + # print('Times', task_times[:200]) + tasks = [asyncio.Task(sleeper(t)) for t in task_times[:200]] loop.run_until_complete(asyncio.Task(watcher(tasks, delay=True))) print() diff --git a/examples/qspeed.py b/examples/qspeed.py index fcd71168..7e92276f 100644 --- a/examples/qspeed.py +++ b/examples/qspeed.py @@ -2,7 +2,10 @@ """How fast is the queue implementation?""" import time + import asyncio + + print(asyncio) N_CONSUMERS = 10 @@ -10,6 +13,7 @@ N_ITEMS = 100000 # Per producer Q_SIZE = 1 + @asyncio.coroutine def producer(q): for i in range(N_ITEMS): @@ -17,6 +21,7 @@ def producer(q): for i in range(N_CONSUMERS): yield from q.put(None) + @asyncio.coroutine def consumer(q): while True: @@ -24,6 +29,7 @@ def consumer(q): if i is None: break + def main(): q = asyncio.Queue(Q_SIZE) loop = asyncio.get_event_loop() @@ -38,6 +44,7 @@ def main(): N_ITEMS, 'items/producer;', Q_SIZE, 'maxsize;', '%.3f total seconds;' % dt, - '%.3f usec per item.' % (1e6*dt/N_ITEMS/N_PRODUCERS)) + '%.3f usec per item.' % (1e6 * dt / N_ITEMS / N_PRODUCERS)) + main() diff --git a/examples/shell.py b/examples/shell.py index f9343256..122091dc 100644 --- a/examples/shell.py +++ b/examples/shell.py @@ -1,9 +1,12 @@ """Examples using create_subprocess_exec() and create_subprocess_shell().""" -import asyncio + import signal + +import asyncio from asyncio.subprocess import PIPE + @asyncio.coroutine def cat(loop): proc = yield from asyncio.create_subprocess_shell("cat", @@ -20,6 +23,7 @@ def cat(loop): exitcode = yield from proc.wait() print("(exit code %s)" % exitcode) + @asyncio.coroutine def ls(loop): proc = yield from asyncio.create_subprocess_exec("ls", @@ -34,6 +38,7 @@ def ls(loop): except ProcessLookupError: pass + @asyncio.coroutine def test_call(*args, timeout=None): proc = yield from asyncio.create_subprocess_exec(*args) @@ -45,6 +50,7 @@ def test_call(*args, timeout=None): proc.kill() yield from proc.wait() + loop = asyncio.get_event_loop() loop.run_until_complete(cat(loop)) loop.run_until_complete(ls(loop)) diff --git a/examples/simple_tcp_server.py b/examples/simple_tcp_server.py index 5f874ffc..cd403c39 100644 --- a/examples/simple_tcp_server.py +++ b/examples/simple_tcp_server.py @@ -9,6 +9,7 @@ """ import sys + import asyncio import asyncio.streams @@ -24,13 +25,13 @@ class MyServer: """ def __init__(self): - self.server = None # encapsulates the server sockets + self.server = None # encapsulates the server sockets # this keeps track of all the clients that connected to our # server. It can be useful in some cases, for instance to # kill client connections or to broadcast some data to all # clients... - self.clients = {} # task -> (reader, writer) + self.clients = {} # task -> (reader, writer) def _accept_client(self, client_reader, client_writer): """ @@ -59,7 +60,7 @@ def _handle_client(self, client_reader, client_writer): """ while True: data = (yield from client_reader.readline()).decode("utf-8") - if not data: # an empty string means the client disconnected + if not data: # an empty string means the client disconnected break cmd, *args = data.rstrip().split(' ') if cmd == 'add': @@ -72,7 +73,7 @@ def _handle_client(self, client_reader, client_writer): msg = args[1] client_writer.write("begin\n".encode("utf-8")) for idx in range(times): - client_writer.write("{}. {}\n".format(idx+1, msg) + client_writer.write("{}. {}\n".format(idx + 1, msg) .encode("utf-8")) client_writer.write("end\n".encode("utf-8")) else: diff --git a/examples/sink.py b/examples/sink.py index 5d01f9b8..641540f6 100644 --- a/examples/sink.py +++ b/examples/sink.py @@ -4,7 +4,8 @@ import os import sys -from asyncio import get_event_loop, coroutine, Protocol +import asyncio + ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( @@ -21,7 +22,7 @@ default=1111, type=int, help='Port number') ARGS.add_argument( '--maxsize', action='store', dest='maxsize', - default=16*1024*1024, type=int, help='Max total data size') + default=16 * 1024 * 1024, type=int, help='Max total data size') server = None args = None @@ -31,7 +32,7 @@ def dprint(*args): print('sink:', *args, file=sys.stderr) -class Service(Protocol): +class Service(asyncio.Protocol): def connection_made(self, tr): dprint('connection from', tr.get_extra_info('peername')) @@ -55,7 +56,7 @@ def connection_lost(self, how): dprint('closed', repr(how)) -@coroutine +@asyncio.coroutine def start(loop, host, port): global server sslctx = None @@ -83,7 +84,7 @@ def main(): loop = ProactorEventLoop() set_event_loop(loop) else: - loop = get_event_loop() + loop = asyncio.get_event_loop() try: loop.run_until_complete(start(loop, args.host, args.port)) finally: diff --git a/examples/source.py b/examples/source.py index 6e5de561..a7c49e85 100644 --- a/examples/source.py +++ b/examples/source.py @@ -3,7 +3,7 @@ import argparse import sys -from asyncio import test_utils, get_event_loop, coroutine, Protocol, Future +import asyncio ARGS = argparse.ArgumentParser(description="TCP data sink example.") @@ -24,7 +24,7 @@ default=1111, type=int, help='Port number') ARGS.add_argument( '--size', action='store', dest='size', - default=16*1024, type=int, help='Data size') + default=16 * 1024, type=int, help='Data size') args = None @@ -33,7 +33,7 @@ def dprint(*args): print('source:', *args, file=sys.stderr) -class Client(Protocol): +class Client(asyncio.Protocol): total = 0 @@ -42,13 +42,13 @@ def connection_made(self, tr): dprint('my socket is', tr.get_extra_info('sockname')) self.tr = tr self.lost = False - self.loop = get_event_loop() - self.waiter = Future() + self.loop = asyncio.get_event_loop() + self.waiter = asyncio.Future() if args.stop: self.tr.write(b'stop') self.tr.close() else: - self.data = b'x'*args.size + self.data = b'x' * args.size self.write_some_data() def write_some_data(self): @@ -68,11 +68,11 @@ def connection_lost(self, exc): self.waiter.set_result(None) -@coroutine +@asyncio.coroutine def start(loop, host, port): sslctx = None if args.tls: - sslctx = test_utils.dummy_ssl_context() + sslctx = asyncio.test_utils.dummy_ssl_context() tr, pr = yield from loop.create_connection(Client, host, port, ssl=sslctx) dprint('tr =', tr) @@ -88,7 +88,7 @@ def main(): loop = ProactorEventLoop() set_event_loop(loop) else: - loop = get_event_loop() + loop = asyncio.get_event_loop() try: loop.run_until_complete(start(loop, args.host, args.port)) finally: diff --git a/examples/source1.py b/examples/source1.py index ee52658a..65938cd0 100644 --- a/examples/source1.py +++ b/examples/source1.py @@ -3,7 +3,8 @@ import argparse import sys -from asyncio import test_utils, get_event_loop, coroutine, open_connection +import asyncio + ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( @@ -23,7 +24,7 @@ default=1111, type=int, help='Port number') ARGS.add_argument( '--size', action='store', dest='size', - default=16*1024, type=int, help='Data size') + default=16 * 1024, type=int, help='Data size') class Debug: @@ -48,15 +49,15 @@ def oprint(self, *args): print(self.label, *args, file=sys.stderr, end=end, flush=True) -@coroutine +@asyncio.coroutine def start(loop, args): d = Debug() total = 0 sslctx = None if args.tls: d.print('using dummy SSLContext') - sslctx = test_utils.dummy_ssl_context() - r, w = yield from open_connection(args.host, args.port, ssl=sslctx) + sslctx = asyncio.test_utils.dummy_ssl_context() + r, w = yield from asyncio.open_connection(args.host, args.port, ssl=sslctx) d.print('r =', r) d.print('w =', w) if args.stop: @@ -64,7 +65,7 @@ def start(loop, args): w.close() else: size = args.size - data = b'x'*size + data = b'x' * size try: while True: total += size @@ -86,7 +87,7 @@ def main(): loop = ProactorEventLoop() set_event_loop(loop) else: - loop = get_event_loop() + loop = asyncio.get_event_loop() try: loop.run_until_complete(start(loop, args)) finally: diff --git a/examples/stacks.py b/examples/stacks.py index ddafa0c6..2c512aa5 100644 --- a/examples/stacks.py +++ b/examples/stacks.py @@ -1,28 +1,30 @@ """Crude demo for print_stack().""" +import asyncio -from asyncio import get_event_loop, open_connection, coroutine, Task, async - -@coroutine +@asyncio.coroutine def helper(r): print('--- helper ---') - for t in Task.all_tasks(): + for t in asyncio.Task.all_tasks(): t.print_stack() print('--- end helper ---') line = yield from r.readline() - 1/0 + 1 / 0 return line + def doit(): - l = get_event_loop() + l = asyncio.get_event_loop() lr = l.run_until_complete - r, w = lr(open_connection('python.org', 80)) - t1 = async(helper(r)) - for t in Task.all_tasks(): t.print_stack() + r, w = lr(asyncio.open_connection('python.org', 80)) + t1 = asyncio.async(helper(r)) + for t in asyncio.Task.all_tasks(): + t.print_stack() print('---') l._run_once() - for t in Task.all_tasks(): t.print_stack() + for t in asyncio.Task.all_tasks(): + t.print_stack() print('---') w.write(b'GET /\r\n') w.write_eof() @@ -31,7 +33,7 @@ def doit(): except Exception as e: print('catching', e) finally: - for t in Task.all_tasks(): + for t in asyncio.Task.all_tasks(): t.print_stack() l.close() diff --git a/examples/subprocess_attach_read_pipe.py b/examples/subprocess_attach_read_pipe.py index d8a62420..918968c3 100644 --- a/examples/subprocess_attach_read_pipe.py +++ b/examples/subprocess_attach_read_pipe.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 """Example showing how to attach a read pipe to a subprocess.""" + +import os +import sys + import asyncio -import os, sys code = """ import os, sys @@ -12,6 +15,7 @@ loop = asyncio.get_event_loop() + @asyncio.coroutine def task(): rfd, wfd = os.pipe() @@ -29,5 +33,6 @@ def task(): data = yield from reader.read() print("read = %r" % data.decode()) + loop.run_until_complete(task()) loop.close() diff --git a/examples/subprocess_attach_write_pipe.py b/examples/subprocess_attach_write_pipe.py index c4e099f6..a3040bca 100644 --- a/examples/subprocess_attach_write_pipe.py +++ b/examples/subprocess_attach_write_pipe.py @@ -1,8 +1,10 @@ #!/usr/bin/env python3 """Example showing how to attach a write pipe to a subprocess.""" + +import os +import sys + import asyncio -import os, sys -from asyncio import subprocess code = """ import os, sys @@ -13,6 +15,7 @@ loop = asyncio.get_event_loop() + @asyncio.coroutine def task(): rfd, wfd = os.pipe() @@ -20,7 +23,7 @@ def task(): proc = yield from asyncio.create_subprocess_exec( *args, pass_fds={rfd}, - stdout=subprocess.PIPE) + stdout=asyncio.subprocess.PIPE) pipe = open(wfd, 'wb', 0) transport, _ = yield from loop.connect_write_pipe(asyncio.Protocol, @@ -31,5 +34,6 @@ def task(): print("stdout = %r" % stdout.decode()) transport.close() + loop.run_until_complete(task()) loop.close() diff --git a/examples/subprocess_shell.py b/examples/subprocess_shell.py index 745cb646..c09891f9 100644 --- a/examples/subprocess_shell.py +++ b/examples/subprocess_shell.py @@ -1,8 +1,9 @@ """Example writing to and reading from a subprocess at the same time using tasks.""" -import asyncio import os + +import asyncio from asyncio.subprocess import PIPE @@ -23,6 +24,7 @@ def send_input(writer, input): except ConnectionResetError: print('stdin: connection reset error') + @asyncio.coroutine def log_errors(reader): while True: @@ -31,6 +33,7 @@ def log_errors(reader): break print('ERROR', repr(line)) + @asyncio.coroutine def read_stdout(stdout): while True: @@ -39,6 +42,7 @@ def read_stdout(stdout): if not line: break + @asyncio.coroutine def start(cmd, input=None, **kwds): kwds['stdout'] = PIPE @@ -79,7 +83,7 @@ def main(): else: loop = asyncio.get_event_loop() loop.run_until_complete(start( - 'sleep 2; wc', input=[b'foo bar baz\n'*300 for i in range(100)])) + 'sleep 2; wc', input=[b'foo bar baz\n' * 300 for i in range(100)])) loop.close() diff --git a/examples/tcp_echo.py b/examples/tcp_echo.py index d743242a..0e11a463 100755 --- a/examples/tcp_echo.py +++ b/examples/tcp_echo.py @@ -1,8 +1,12 @@ #!/usr/bin/env python3 """TCP echo server example.""" + import argparse -import asyncio import sys + +import asyncio + + try: import signal except ImportError: diff --git a/examples/timing_tcp_server.py b/examples/timing_tcp_server.py index 3fcdc974..b3962436 100644 --- a/examples/timing_tcp_server.py +++ b/examples/timing_tcp_server.py @@ -27,13 +27,13 @@ class MyServer: """ def __init__(self): - self.server = None # encapsulates the server sockets + self.server = None # encapsulates the server sockets # this keeps track of all the clients that connected to our # server. It can be useful in some cases, for instance to # kill client connections or to broadcast some data to all # clients... - self.clients = {} # task -> (reader, writer) + self.clients = {} # task -> (reader, writer) def _accept_client(self, client_reader, client_writer): """ @@ -62,7 +62,7 @@ def _handle_client(self, client_reader, client_writer): """ while True: data = (yield from client_reader.readline()).decode("utf-8") - if not data: # an empty string means the client disconnected + if not data: # an empty string means the client disconnected break cmd, *args = data.rstrip().split(' ') if cmd == 'add': diff --git a/examples/udp_echo.py b/examples/udp_echo.py index 93ac7e6b..d8f71ed9 100755 --- a/examples/udp_echo.py +++ b/examples/udp_echo.py @@ -1,8 +1,12 @@ #!/usr/bin/env python3 """UDP echo example.""" + import argparse import sys + import asyncio + + try: import signal except ImportError: