V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
kidlfy
V2EX  ›  Python

Python gevent 的异步请求 return 的返回值怎么得到

  •  
  •   kidlfy · 2018-09-13 11:14:16 +08:00 · 4485 次点击
    这是一个创建于 2045 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import gevent.monkey
    gevent.monkey.patch_socket()

    import gevent
    import urllib2
    import simplejson as json

    def fetch(pid):
    response = urllib2.urlopen('http://json-time.appspot.com/time.json')
    result = response.read()
    json_result = json.loads(result)
    datetime = json_result['datetime']
    print 'Process ', pid, datetime
    return json_result['datetime']

    def asynchronous():
    threads = []
    for i in range(1,10):
    threads.append(gevent.spawn(fetch, i))
    gevent.joinall(threads)

    我想得到 fetch 里面那个 return 的返回值,在 gevent.spawn 的情况下怎么才能得到 spawn 的函数里 return 的值啊
    7 条回复    2018-09-14 18:35:36 +08:00
    uwh0am1
        1
    uwh0am1  
       2018-09-13 13:09:23 +08:00
    kidlfy
        2
    kidlfy  
    OP
       2018-09-13 14:00:12 +08:00
    或者说,我并不太理解在并发执行函数时怎么能得到每个函数的返回值? 不用 gevent 有别的方法吗
    xpresslink
        3
    xpresslink  
       2018-09-13 15:30:16 +08:00
    这么简单问题还用问?
    import queue

    q = queue.Queue(maxsize=0)

    把 return 改成

    q.put( json_result['datetime'])

    用 q.get()一个一个取,或者 list(q.queue) 一起取
    kidlfy
        4
    kidlfy  
    OP
       2018-09-13 15:47:08 +08:00
    @xpresslink 我就是这么取的,只是想知道 return 能不能 直接得到
    xpresslink
        5
    xpresslink  
       2018-09-14 11:01:19 +08:00
    @kidlfy
    你 return 给谁呀?
    那样就成了顺序调用无法并发了。
    kidlfy
        6
    kidlfy  
    OP
       2018-09-14 13:40:38 +08:00
    @xpresslink 嗯嗯,我理解一点了,谢谢
    fzzff
        7
    fzzff  
       2018-09-14 18:35:36 +08:00
    from gevent import monkey
    monkey.patch_socket()
    monkey.patch_ssl()
    import gevent
    import requests
    import simplejson as json
    from tornado import gen

    @gen.coroutine
    def fetch(pid):
    response = requests.get('https://www.baidu.com')
    result = response.text
    # json_result = json.loads(result)
    # datetime = json_result['datetime']
    print('Process ', result)
    raise gen.Return(result)
    # return result

    def asynchronous():
    threads = []
    for i in range(1,10):
    threads.append(gevent.spawn(fetch, i))
    gevent.joinall(threads)

    可以这样
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5369 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 06:43 · PVG 14:43 · LAX 23:43 · JFK 02:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.