From b0729fe75b6bc35054f77b01e72fe8dfede5ef17 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 16 Oct 2013 12:03:07 -0700 Subject: Update examples to use asyncio, not tulip. --- examples/child_process.py | 40 ++++++++++++++++++++-------------------- examples/fetch0.py | 2 +- examples/fetch1.py | 2 +- examples/fetch2.py | 2 +- examples/fetch3.py | 2 +- examples/sink.py | 4 ++-- examples/source.py | 4 ++-- examples/stacks.py | 2 +- examples/tcp_echo.py | 18 +++++++++--------- examples/udp_echo.py | 10 +++++----- 10 files changed, 43 insertions(+), 43 deletions(-) (limited to 'examples') diff --git a/examples/child_process.py b/examples/child_process.py index 5a88faa..ef31e68 100644 --- a/examples/child_process.py +++ b/examples/child_process.py @@ -8,18 +8,18 @@ import os import sys try: - import tulip + import asyncio except ImportError: - # tulip is not installed + # asyncio is not installed sys.path.append(os.path.join(os.path.dirname(__file__), '..')) - import tulip + import asyncio -from tulip import streams -from tulip import protocols +from asyncio import streams +from asyncio import protocols if sys.platform == 'win32': - from tulip.windows_utils import Popen, PIPE - from tulip.windows_events import ProactorEventLoop + from asyncio.windows_utils import Popen, PIPE + from asyncio.windows_events import ProactorEventLoop else: from subprocess import Popen, PIPE @@ -27,20 +27,20 @@ else: # Return a write-only transport wrapping a writable pipe # -@tulip.coroutine +@asyncio.coroutine def connect_write_pipe(file): - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() protocol = protocols.Protocol() - transport, _ = yield from loop.connect_write_pipe(tulip.Protocol, file) + transport, _ = yield from loop.connect_write_pipe(asyncio.Protocol, file) return transport # # Wrap a readable pipe in a stream # -@tulip.coroutine +@asyncio.coroutine def connect_read_pipe(file): - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() stream_reader = streams.StreamReader(loop=loop) def factory(): return streams.StreamReaderProtocol(stream_reader) @@ -52,7 +52,7 @@ def connect_read_pipe(file): # Example # -@tulip.coroutine +@asyncio.coroutine def main(loop): # program which prints evaluation of each expression from stdin code = r'''if 1: @@ -88,8 +88,8 @@ def main(loop): # interact with subprocess name = {stdout:'OUT', stderr:'ERR'} - registered = {tulip.Task(stderr.readline()): stderr, - tulip.Task(stdout.readline()): stdout} + registered = {asyncio.Task(stderr.readline()): stderr, + asyncio.Task(stdout.readline()): stdout} while registered: # write command cmd = next(commands, None) @@ -102,8 +102,8 @@ def main(loop): # get and print lines from stdout, stderr timeout = None while registered: - done, pending = yield from tulip.wait( - registered, timeout=timeout, return_when=tulip.FIRST_COMPLETED) + done, pending = yield from asyncio.wait( + registered, timeout=timeout, return_when=asyncio.FIRST_COMPLETED) if not done: break for f in done: @@ -111,7 +111,7 @@ def main(loop): res = f.result() print(name[stream], res.decode('ascii').rstrip()) if res != b'': - registered[tulip.Task(stream.readline())] = stream + registered[asyncio.Task(stream.readline())] = stream timeout = 0.0 stdout_transport.close() @@ -120,8 +120,8 @@ def main(loop): if __name__ == '__main__': if sys.platform == 'win32': loop = ProactorEventLoop() - tulip.set_event_loop(loop) + asyncio.set_event_loop(loop) else: - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() loop.run_until_complete(main(loop)) loop.close() diff --git a/examples/fetch0.py b/examples/fetch0.py index 84edaa2..ac4d5d9 100644 --- a/examples/fetch0.py +++ b/examples/fetch0.py @@ -2,7 +2,7 @@ import sys -from tulip import * +from asyncio import * @coroutine diff --git a/examples/fetch1.py b/examples/fetch1.py index 57e66e6..6d99262 100644 --- a/examples/fetch1.py +++ b/examples/fetch1.py @@ -6,7 +6,7 @@ This version adds URL parsing (including SSL) and a Response object. import sys import urllib.parse -from tulip import * +from asyncio import * class Response: diff --git a/examples/fetch2.py b/examples/fetch2.py index ca250d6..0899123 100644 --- a/examples/fetch2.py +++ b/examples/fetch2.py @@ -7,7 +7,7 @@ import sys import urllib.parse from http.client import BadStatusLine -from tulip import * +from asyncio import * class Request: diff --git a/examples/fetch3.py b/examples/fetch3.py index 3b2c8ae..fac880f 100644 --- a/examples/fetch3.py +++ b/examples/fetch3.py @@ -8,7 +8,7 @@ import sys import urllib.parse from http.client import BadStatusLine -from tulip import * +from asyncio import * class ConnectionPool: diff --git a/examples/sink.py b/examples/sink.py index 855a4aa..bb29be2 100644 --- a/examples/sink.py +++ b/examples/sink.py @@ -2,7 +2,7 @@ import sys -from tulip import * +from asyncio import * server = None @@ -41,7 +41,7 @@ def start(loop): def main(): if '--iocp' in sys.argv: - from tulip.windows_events import ProactorEventLoop + from asyncio.windows_events import ProactorEventLoop loop = ProactorEventLoop() set_event_loop(loop) loop = get_event_loop() diff --git a/examples/source.py b/examples/source.py index 7a5011d..6bfdcb0 100644 --- a/examples/source.py +++ b/examples/source.py @@ -2,7 +2,7 @@ import sys -from tulip import * +from asyncio import * def dprint(*args): print('source:', *args, file=sys.stderr) @@ -47,7 +47,7 @@ def start(loop): def main(): if '--iocp' in sys.argv: - from tulip.windows_events import ProactorEventLoop + from asyncio.windows_events import ProactorEventLoop loop = ProactorEventLoop() set_event_loop(loop) loop = get_event_loop() diff --git a/examples/stacks.py b/examples/stacks.py index 77a99cf..371d31f 100644 --- a/examples/stacks.py +++ b/examples/stacks.py @@ -1,7 +1,7 @@ """Crude demo for print_stack().""" -from tulip import * +from asyncio import * @coroutine diff --git a/examples/tcp_echo.py b/examples/tcp_echo.py index 39db5cc..6082ef7 100755 --- a/examples/tcp_echo.py +++ b/examples/tcp_echo.py @@ -1,14 +1,14 @@ #!/usr/bin/env python3 """TCP echo server example.""" import argparse -import tulip +import asyncio try: import signal except ImportError: signal = None -class EchoServer(tulip.Protocol): +class EchoServer(asyncio.Protocol): TIMEOUT = 5.0 @@ -21,7 +21,7 @@ class EchoServer(tulip.Protocol): self.transport = transport # start 5 seconds timeout timer - self.h_timeout = tulip.get_event_loop().call_later( + self.h_timeout = asyncio.get_event_loop().call_later( self.TIMEOUT, self.timeout) def data_received(self, data): @@ -30,7 +30,7 @@ class EchoServer(tulip.Protocol): # restart timeout timer self.h_timeout.cancel() - self.h_timeout = tulip.get_event_loop().call_later( + self.h_timeout = asyncio.get_event_loop().call_later( self.TIMEOUT, self.timeout) def eof_received(self): @@ -41,7 +41,7 @@ class EchoServer(tulip.Protocol): self.h_timeout.cancel() -class EchoClient(tulip.Protocol): +class EchoClient(asyncio.Protocol): message = 'This is the message. It will be echoed.' @@ -54,18 +54,18 @@ class EchoClient(tulip.Protocol): print('data received:', data) # disconnect after 10 seconds - tulip.get_event_loop().call_later(10.0, self.transport.close) + asyncio.get_event_loop().call_later(10.0, self.transport.close) def eof_received(self): pass def connection_lost(self, exc): print('connection lost:', exc) - tulip.get_event_loop().stop() + asyncio.get_event_loop().stop() def start_client(loop, host, port): - t = tulip.Task(loop.create_connection(EchoClient, host, port)) + t = asyncio.Task(loop.create_connection(EchoClient, host, port)) loop.run_until_complete(t) @@ -101,7 +101,7 @@ if __name__ == '__main__': print('Please specify --server or --client\n') ARGS.print_help() else: - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() if signal is not None: loop.add_signal_handler(signal.SIGINT, loop.stop) diff --git a/examples/udp_echo.py b/examples/udp_echo.py index 0347bfb..8e95d29 100755 --- a/examples/udp_echo.py +++ b/examples/udp_echo.py @@ -2,7 +2,7 @@ """UDP echo example.""" import argparse import sys -import tulip +import asyncio try: import signal except ImportError: @@ -45,18 +45,18 @@ class MyClientUdpEchoProtocol: def connection_lost(self, exc): print('closing transport', exc) - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() loop.stop() def start_server(loop, addr): - t = tulip.Task(loop.create_datagram_endpoint( + t = asyncio.Task(loop.create_datagram_endpoint( MyServerUdpEchoProtocol, local_addr=addr)) loop.run_until_complete(t) def start_client(loop, addr): - t = tulip.Task(loop.create_datagram_endpoint( + t = asyncio.Task(loop.create_datagram_endpoint( MyClientUdpEchoProtocol, remote_addr=addr)) loop.run_until_complete(t) @@ -86,7 +86,7 @@ if __name__ == '__main__': print('Please specify --server or --client\n') ARGS.print_help() else: - loop = tulip.get_event_loop() + loop = asyncio.get_event_loop() if signal is not None: loop.add_signal_handler(signal.SIGINT, loop.stop) -- cgit v1.2.1