Example concurrent_download.py¶
1#!/usr/bin/python
2# Copyright (c) 2009 Denis Bilenko. See LICENSE for details.
3# gevent-test-requires-resource: network
4"""Spawn multiple workers and wait for them to complete"""
5from __future__ import print_function
6import gevent
7from gevent import monkey
8
9# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
10monkey.patch_all()
11
12import requests
13
14# Note that we're using HTTPS, so
15# this demonstrates that SSL works.
16urls = [
17 'https://www.google.com/',
18 'https://www.apple.com/',
19 'https://www.python.org/'
20]
21
22
23
24def print_head(url):
25 print('Starting %s' % url)
26 data = requests.get(url).text
27 print('%s: %s bytes: %r' % (url, len(data), data[:50]))
28
29jobs = [gevent.spawn(print_head, _url) for _url in urls]
30
31gevent.wait(jobs)