Created
February 14, 2017 15:47
-
-
Save ke4roh/f19cdc704559b5ea482533636e345167 to your computer and use it in GitHub Desktop.
Circuits framework parallel fetching example - broken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
from circuits.web.client import Client, request as request_event | |
from circuits.web import Server, Controller | |
from circuits import handler, Debugger | |
class Root(Controller): | |
channel = "web" | |
def __init__(self): | |
super().__init__() | |
@handler("request", priority=0.3) | |
def _on_request(self, event, request, response): | |
if request.path != "/": | |
print(request.path) | |
return | |
urls = [ | |
'https://www.w3.org/services/html2txt?url=http%3A%2F%2Fwww.example.com%2F', | |
'http://home.hiwaay.net/~jimes/checklist.txt' | |
] | |
events = [] | |
results = [] | |
for url in urls: | |
event = request_event('GET', url) | |
events.append(event) | |
self.fire(event, 'url-fetching') | |
# for event in events: | |
er = yield self.waitEvent(event, 'url-fetching') | |
results.append(er) | |
response.headers["Content-Type"] = "text/plain" | |
response.body = " ".join([r.value.read().decode("utf-8") for r in results]) | |
try: | |
return response | |
finally: | |
event.stop() | |
(Server(8011) + Root() + Client(channel='url-fetching') + Debugger()).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment