Change import statements to import only what's needed by denisra · Pull Request #468 · python/asyncio · GitHub
Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/cacheclt.py
6 changes: 5 additions & 1 deletion examples/child_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -116,6 +119,7 @@ def writeall(fd, buf):
stdout_transport.close()
stderr_transport.close()


if __name__ == '__main__':
if sys.platform == 'win32':
loop = ProactorEventLoop()
Expand Down
6 changes: 3 additions & 3 deletions examples/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions examples/echo_client_tulip.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -15,6 +17,7 @@ def echo_client():
break
writer.close()


loop = asyncio.get_event_loop()
loop.run_until_complete(echo_client())
loop.close()
3 changes: 3 additions & 0 deletions examples/echo_server_tulip.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions examples/fetch0.py
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -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:
Expand Down
22 changes: 12 additions & 10 deletions examples/fetch1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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':
Expand All @@ -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)
Expand All @@ -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:
Expand Down
38 changes: 21 additions & 17 deletions examples/fetch2.py
Loading