Python 请求:不要等待请求完成 [英] Python Requests: Don't wait for request to finish

查看:46
本文介绍了Python 请求:不要等待请求完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Bash 中,可以通过附加 & 在后台执行命令.我如何在 Python 中做到这一点?

In Bash, it is possible to execute a command in the background by appending &. How can I do it in Python?

while True:
    data = raw_input('Enter something: ') 
    requests.post(url, data=data) # Don't wait for it to finish.
    print('Sending POST request...') # This should appear immediately.

推荐答案

我使用 multiprocessing.dummy.Pool.我在模块级别创建了一个单例线程池,然后使用 pool.apply_async(requests.get, [params]) 来启动任务.

I use multiprocessing.dummy.Pool. I create a singleton thread pool at the module level, and then use pool.apply_async(requests.get, [params]) to launch the task.

这个命令给了我一个未来,我可以无限期地将它与其他未来一起添加到列表中,直到我想要收集全部或部分结果.

This command gives me a future, which I can add to a list with other futures indefinitely until I'd like to collect all or some of the results.

multiprocessing.dummy.Pool 违背所有逻辑和理由,是线程池而不是进程池.

multiprocessing.dummy.Pool is, against all logic and reason, a THREAD pool and not a process pool.

示例(适用于 Python 2 和 3,只要安装了 requests):

Example (works in both Python 2 and 3, as long as requests is installed):

from multiprocessing.dummy import Pool

import requests

pool = Pool(10) # Creates a pool with ten threads; more threads = more concurrency.
                # "pool" is a module attribute; you can be sure there will only
                # be one of them in your application
                # as modules are cached after initialization.

if __name__ == '__main__':
    futures = []
    for x in range(10):
        futures.append(pool.apply_async(requests.get, ['http://example.com/']))
    # futures is now a list of 10 futures.
    for future in futures:
        print(future.get()) # For each future, wait until the request is
                            # finished and then print the response object.

请求将并发执行,因此运行所有十个请求的时间不应超过最长的一个.这种策略只会使用一个 CPU 内核,但这应该不是问题,因为几乎所有的时间都将花费在等待 I/O 上.

The requests will be executed concurrently, so running all ten of these requests should take no longer than the longest one. This strategy will only use one CPU core, but that shouldn't be an issue because almost all of the time will be spent waiting for I/O.

这篇关于Python 请求:不要等待请求完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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