| 1 |
================================== |
| 2 |
Example using the gevent Pool |
| 3 |
================================== |
| 4 |
|
| 5 |
Introduction |
| 6 |
============ |
| 7 |
|
| 8 |
This is a Celery application containing two example tasks. |
| 9 |
|
| 10 |
First you need to install gevent:: |
| 11 |
|
| 12 |
$ python -m pip install gevent celery pybloom-live |
| 13 |
|
| 14 |
Before you run any of the example tasks you need to start |
| 15 |
the worker:: |
| 16 |
|
| 17 |
$ cd examples/gevent |
| 18 |
$ celery worker -l INFO --concurrency=500 --pool=gevent |
| 19 |
|
| 20 |
As usual you need to have RabbitMQ running, see the Celery getting started |
| 21 |
guide if you haven't installed it yet. |
| 22 |
|
| 23 |
Tasks |
| 24 |
===== |
| 25 |
|
| 26 |
* `tasks.urlopen` |
| 27 |
|
| 28 |
This task simply makes a request opening the URL and returns the size |
| 29 |
of the response body:: |
| 30 |
|
| 31 |
$ cd examples/gevent |
| 32 |
$ python |
| 33 |
>>> from tasks import urlopen |
| 34 |
>>> urlopen.delay('https://www.google.com/').get() |
| 35 |
9980 |
| 36 |
|
| 37 |
To open several URLs at once you can do:: |
| 38 |
|
| 39 |
$ cd examples/gevent |
| 40 |
$ python |
| 41 |
>>> from tasks import urlopen |
| 42 |
>>> from celery import group |
| 43 |
>>> result = group(urlopen.s(url) |
| 44 |
... for url in LIST_OF_URLS).apply_async() |
| 45 |
>>> for incoming_result in result.iter_native(): |
| 46 |
... print(incoming_result) |
| 47 |
|
| 48 |
|
| 49 |
This is a simple recursive web crawler. It will only crawl |
| 50 |
URLs for the current host name. Please see comments in the |
| 51 |
`webcrawler.py` file. |