File: concurrent_download.py

package info (click to toggle)
python-gevent 1.1.1-2~bpo8%2B1
  • links: PTS
  • area: main
  • in suites: jessie-backports
  • size: 12,472 kB
  • sloc: python: 73,749; ansic: 26,302; sh: 11,090; makefile: 927; awk: 18
file content (30 lines) | stat: -rwxr-xr-x 746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
# Copyright (c) 2009 Denis Bilenko. See LICENSE for details.

"""Spawn multiple workers and wait for them to complete"""
from __future__ import print_function
import gevent
from gevent import monkey

# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
monkey.patch_all()

import sys

urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']


if sys.version_info[0] == 3:
    from urllib.request import urlopen
else:
    from urllib2 import urlopen


def print_head(url):
    print('Starting %s' % url)
    data = urlopen(url).read()
    print('%s: %s bytes: %r' % (url, len(data), data[:50]))

jobs = [gevent.spawn(print_head, url) for url in urls]

gevent.wait(jobs)