Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Created November 25, 2015 16:15
Show Gist options
  • Save gnrfan/5157fb299cac06f1f3ff to your computer and use it in GitHub Desktop.
Save gnrfan/5157fb299cac06f1f3ff to your computer and use it in GitHub Desktop.
Playing with goroutines in Python with Goless
#!/usr/env/bin python
# -*- coding: utf8 -*-
import random
import requests
import goless
MIN = 10000
MAX = 99999
def get_size(channel, url, comment=None):
try:
resp = requests.get(url)
result = ('SUCCESS', url, len(resp.content), comment)
except requests.exceptions.RequestException as e:
result = ('ERROR', url, e, comment)
channel.send(result)
def report(result):
status, url, body, comment = result
if status == 'SUCCESS':
print "Document at url %s is %d byte(s) long" % (url, body)
print "Comment: %s" % comment
else:
print "Request for document at url %s failed :(" % url
print "Comment: %s" % comment
if __name__ == '__main__':
c1 = goless.chan()
for n in range(10):
number = random.randint(MIN, MAX + 1)
url = 'https://www.google.com.pe/search?q=%d' % number
params = {}
params['channel'] = c1
params['url'] = url
params['comment'] = 'Searching for number %d' % number
print "Launching %s" % url
goless.go(get_size, **params)
while True:
case, result = goless.select([goless.rcase(c1)])
report(result)
gevent==1.0.2
goless==0.7.2
greenlet==0.4.9
requests==2.8.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment