V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
867810107
V2EX  ›  Python

asyncio 怎么与 redis 队列协同?

  •  
  •   867810107 · Sep 13, 2018 · 3038 views
    This topic created in 2792 days ago, the information mentioned may be changed or developed.

    问题描述

    我有一个进程会不断地往 redis 队列里增加数据,上限是 5000.然后我想在另一个进程中用 asyncio 从该队列里取出 item,里面有 url 然后请求 api 获得数据。请问我应该怎么控制我的爬取进程每一次只运行一定数目的协程(比如 20 )呢?具体应该怎么写呢?就是怎么从队列获得数据然后发起请求,同时又不能同时运行太多协程。

    3 replies    2018-09-16 16:20:16 +08:00
    gaius
        1
    gaius  
       Sep 13, 2018
    单线程定时取 redis,然后丢线程池运行请求 task ?
    ifoolish
        2
    ifoolish  
       Sep 13, 2018 via iPhone
    用 asyncio.Semaphore 控制协程数
    kuokyong
        3
    kuokyong  
       Sep 16, 2018
    第一:需要异步库确保 IO 是异步的 https://github.com/aio-libs/aioredis
    第二控制,协程数据和其它多进程 /线程,调度方式类似。一种是提前创建固定数量协程,不断去读数据。 另外就是两个数据建个调度个协程,.create_task/ensure_future. 达到上限就停止,或者用 Semaphore
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1465 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 16:52 · PVG 00:52 · LAX 09:52 · JFK 12:52
    ♥ Do have faith in what you're doing.