diff --git a/examples/cacheclt.py b/examples/cacheclt.py index b11a4d1a..8f787ada 100644 --- a/examples/cacheclt.py +++ b/examples/cacheclt.py @@ -5,10 +5,11 @@ import argparse import asyncio -from asyncio import test_utils +import asyncio.test_utils import json import logging + 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 @@ -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/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..8024f840 100644 --- a/examples/crawl.py +++ b/examples/crawl.py @@ -341,7 +341,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 +519,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 +534,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 180fcf26..3c71945e 100644 --- a/examples/fetch0.py +++ b/examples/fetch0.py @@ -1,13 +1,12 @@ """Simplest possible HTTP client.""" +import asyncio import sys -from asyncio import * - -@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 +22,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 8dbb6e47..33e065cb 100644 --- a/examples/fetch1.py +++ b/examples/fetch1.py @@ -3,11 +3,10 @@ This version adds URL parsing (including SSL) and a Response object. """ +import asyncio import sys import urllib.parse -from asyncio import * - class Response: @@ -18,27 +17,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 +59,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 +68,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 7617b59b..cd157710 100644 --- a/examples/fetch2.py +++ b/examples/fetch2.py @@ -3,11 +3,10 @@ This version adds a Request object. """ +import asyncio +from http.client import BadStatusLine import sys import urllib.parse -from http.client import BadStatusLine - -from asyncio import * class Request: @@ -34,13 +33,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 +50,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 +82,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 +100,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 +122,7 @@ def read(self): return body -@coroutine +@asyncio.coroutine def fetch(url, verbose=True): request = Request(url, verbose) yield from request.connect() @@ -129,7 +133,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 9419afd2..df6036d1 100644 --- a/examples/fetch3.py +++ b/examples/fetch3.py @@ -4,11 +4,10 @@ chunked transfer-encoding. It also supports a --iocp flag. """ +import asyncio +from http.client import BadStatusLine import sys import urllib.parse -from http.client import BadStatusLine - -from asyncio import * class ConnectionPool: @@ -22,10 +21,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 +40,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 +77,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 +88,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 +105,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 +126,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 +160,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 +191,7 @@ def read(self): return body -@coroutine +@asyncio.coroutine def fetch(url, verbose=True, max_redirect=10): pool = ConnectionPool(verbose) try: @@ -218,7 +217,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..2735cbbf 100644 --- a/examples/fuzz_as_completed.py +++ b/examples/fuzz_as_completed.py @@ -7,13 +7,15 @@ import random import sys + @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 +24,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 +33,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 +64,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..6305b75e 100644 --- a/examples/qspeed.py +++ b/examples/qspeed.py @@ -1,8 +1,10 @@ #!/usr/bin/env python3 """How fast is the queue implementation?""" -import time import asyncio +import time + + print(asyncio) N_CONSUMERS = 10 @@ -10,6 +12,7 @@ N_ITEMS = 100000 # Per producer Q_SIZE = 1 + @asyncio.coroutine def producer(q): for i in range(N_ITEMS): @@ -17,6 +20,7 @@ def producer(q): for i in range(N_CONSUMERS): yield from q.put(None) + @asyncio.coroutine def consumer(q): while True: @@ -24,6 +28,7 @@ def consumer(q): if i is None: break + def main(): q = asyncio.Queue(Q_SIZE) loop = asyncio.get_event_loop() @@ -38,6 +43,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..f1459ec0 100644 --- a/examples/shell.py +++ b/examples/shell.py @@ -1,8 +1,10 @@ """Examples using create_subprocess_exec() and create_subprocess_shell().""" + import asyncio -import signal from asyncio.subprocess import PIPE +import signal + @asyncio.coroutine def cat(loop): @@ -20,6 +22,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 +37,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 +49,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..d94857a3 100644 --- a/examples/simple_tcp_server.py +++ b/examples/simple_tcp_server.py @@ -8,9 +8,9 @@ fail if this port is currently in use. """ -import sys import asyncio import asyncio.streams +import sys class MyServer: @@ -24,13 +24,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 +59,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 +72,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 d362cbb2..6a96cbba 100644 --- a/examples/sink.py +++ b/examples/sink.py @@ -1,10 +1,10 @@ """Test service that accepts connections and reads all data off them.""" import argparse +import asyncio import os import sys -from asyncio import * ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( @@ -21,7 +21,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 +31,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 +55,7 @@ def connection_lost(self, how): dprint('closed', repr(how)) -@coroutine +@asyncio.coroutine def start(loop, host, port): global server sslctx = None @@ -83,7 +83,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 7fd11fb0..cd15ed38 100644 --- a/examples/source.py +++ b/examples/source.py @@ -1,11 +1,10 @@ """Test client that connects and sends infinite data.""" import argparse +import asyncio +import asyncio.test_utils import sys -from asyncio import * -from asyncio import test_utils - ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( @@ -25,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 @@ -34,7 +33,7 @@ def dprint(*args): print('source:', *args, file=sys.stderr) -class Client(Protocol): +class Client(asyncio.Protocol): total = 0 @@ -43,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): @@ -69,7 +68,7 @@ def connection_lost(self, exc): self.waiter.set_result(None) -@coroutine +@asyncio.coroutine def start(loop, host, port): sslctx = None if args.tls: @@ -89,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 6802e963..ee8d4039 100644 --- a/examples/source1.py +++ b/examples/source1.py @@ -1,10 +1,10 @@ """Like source.py, but uses streams.""" import argparse +import asyncio +import asyncio.test_utils import sys -from asyncio import * -from asyncio import test_utils ARGS = argparse.ArgumentParser(description="TCP data sink example.") ARGS.add_argument( @@ -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') class Debug: @@ -49,7 +49,7 @@ 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 @@ -57,7 +57,7 @@ def start(loop, args): 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) + r, w = yield from asyncio.open_connection(args.host, args.port, ssl=sslctx) d.print('r =', r) d.print('w =', w) if args.stop: @@ -65,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 @@ -87,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 0b7e0b2c..2c512aa5 100644 --- a/examples/stacks.py +++ b/examples/stacks.py @@ -1,28 +1,30 @@ """Crude demo for print_stack().""" +import asyncio -from asyncio import * - -@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..c9092a83 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 asyncio -import os, sys +import os +import 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..8fef1cfd 100644 --- a/examples/subprocess_attach_write_pipe.py +++ b/examples/subprocess_attach_write_pipe.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 """Example showing how to attach a write pipe to a subprocess.""" + import asyncio -import os, sys -from asyncio import subprocess +from asyncio.subprocess import PIPE +import os +import sys + code = """ import os, sys @@ -13,6 +16,7 @@ loop = asyncio.get_event_loop() + @asyncio.coroutine def task(): rfd, wfd = os.pipe() @@ -20,7 +24,7 @@ def task(): proc = yield from asyncio.create_subprocess_exec( *args, pass_fds={rfd}, - stdout=subprocess.PIPE) + stdout=PIPE) pipe = open(wfd, 'wb', 0) transport, _ = yield from loop.connect_write_pipe(asyncio.Protocol, @@ -31,5 +35,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..8ba4407d 100644 --- a/examples/subprocess_shell.py +++ b/examples/subprocess_shell.py @@ -2,8 +2,8 @@ tasks.""" import asyncio -import os from asyncio.subprocess import PIPE +import os @asyncio.coroutine @@ -23,6 +23,7 @@ def send_input(writer, input): except ConnectionResetError: print('stdin: connection reset error') + @asyncio.coroutine def log_errors(reader): while True: @@ -31,6 +32,7 @@ def log_errors(reader): break print('ERROR', repr(line)) + @asyncio.coroutine def read_stdout(stdout): while True: @@ -39,6 +41,7 @@ def read_stdout(stdout): if not line: break + @asyncio.coroutine def start(cmd, input=None, **kwds): kwds['stdout'] = PIPE @@ -79,7 +82,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..3b98f080 100755 --- a/examples/tcp_echo.py +++ b/examples/tcp_echo.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 """TCP echo server example.""" + import argparse import asyncio import sys + + try: import signal except ImportError: diff --git a/examples/timing_tcp_server.py b/examples/timing_tcp_server.py index 3fcdc974..85cf72b7 100644 --- a/examples/timing_tcp_server.py +++ b/examples/timing_tcp_server.py @@ -8,12 +8,11 @@ fail if this port is currently in use. """ -import sys -import time -import random - import asyncio import asyncio.streams +import random +import sys +import time class MyServer: @@ -27,13 +26,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 +61,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..830c6fb7 100755 --- a/examples/udp_echo.py +++ b/examples/udp_echo.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 """UDP echo example.""" + import argparse -import sys import asyncio +import sys + + try: import signal except ImportError: