使用请求和多处理时出现的奇怪问题 [英] Strange problems when using requests and multiprocessing

查看:195
本文介绍了使用请求和多处理时出现的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查以下python代码:

Please check this python code:

#!/usr/bin/env python
import requests
import multiprocessing
from time import sleep, time
from requests import async

def do_req():
    r = requests.get("http://w3c.org/")

def do_sth():    
    while True:
        sleep(10)

if __name__ == '__main__':        
    do_req()
    multiprocessing.Process( target=do_sth, args=() ).start()

当我按Ctrl-C(运行后等待2秒钟-让Process运行)时,它不会停止.当我将导入顺序更改为:

When I press Ctrl-C (wait 2sec after run - let Process run), it doesn't stop. When I change the import order to:

from requests import async
from time import sleep, time

它在Ctrl-C之后停止.为什么在第一个示例中它不会停止/杀死?

it stops after Ctrl-C. Why it doesn't stop/kill in first example?

是错误还是功能?

注意:

  • 是的,我知道,我没有在代码中使用异步,这只是精简的代码.在实际代码中,我使用它.我这样做是为了简化我的问题.
  • 按Ctrl-C后,将运行一个新的(子)进程.为什么?
  • multiprocessing.__version__ == 0.70a1requests.__version__ == 0.11.2gevent.__version__ == 0.13.7
  • Yes I know, that I didn't use async in this code, this is just stripped down code. In real code I use it. I did it to simplify my question.
  • After pressing Ctrl-C there is a new (child) process running. Why?
  • multiprocessing.__version__ == 0.70a1, requests.__version__ == 0.11.2, gevent.__version__ == 0.13.7

推荐答案

请求异步模块使用gevent.如果您查看gevent的源代码,您会发现它是 monkey补丁的许多Python标准库函数,包括sleep:

Requests async module uses gevent. If you look at the source code of gevent you will see that it monkey patches many of Python's standard library functions, including sleep:

request.async模块在导入期间执行:

request.async module during import executes:

    from gevent import monkey as curious_george
    # Monkey-patch.
    curious_george.patch_all(thread=False, select=False)

查看gevent的monkey.py模块,您会看到:

Looking at the monkey.py module of gevent you can see:

https://bitbucket.org/denis/gevent/src/f838056c793d /gevent/monkey.py#cl-128

def patch_time():
    """Replace :func:`time.sleep` with :func:`gevent.sleep`."""
    from gevent.hub import sleep
    import time
    patch_item(time, 'sleep', sleep)

查看gevent存储库中的代码以了解详细信息.

Take a look at the code from the gevent's repository for details.

这篇关于使用请求和多处理时出现的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆