Skip to content

Instantly share code, notes, and snippets.

@ke4roh
Created February 14, 2017 15:47
Show Gist options
  • Save ke4roh/f19cdc704559b5ea482533636e345167 to your computer and use it in GitHub Desktop.
Save ke4roh/f19cdc704559b5ea482533636e345167 to your computer and use it in GitHub Desktop.
Circuits framework parallel fetching example - broken
#!/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