使用 uWSGI、Pyramid 的多线程 requests.post

Multi threading requests.post using uWSGI, Pyramid

我正在尝试通过 cron 作业在多线程中请求 API。我看起来想停止或延迟线程请求。 pserve没问题。我想使用 uWSGI 但我遇到了一些问题。

Python 3.5.2

我的代码是这样的:

import threading
import requests

def worker(settings):
    lookup_url = settings['lookup_url']
    api_sid = settings['api_sid']
    auth_token = settings['auth_token']
    args = settings['args']
    resp = requests.post(lookup_url,
                         auth=(api_sid, auth_token),
                         data={'data': args},
                         timeout=5.0,
                         )

def main(request):
    registry = request.registry
    settings = registry.settings
    for _ in range(3):
        threading.Thread(target=worker, args=(settings,)).start()

请让我知道任何解决方案。

UWSGI默认不启用Python个线程:

By default the Python plugin does not initialize the GIL. This means your app-generated threads will not run. If you need threads, remember to enable them with enable-threads. Running uWSGI in multithreading mode (with the threads options) will automatically enable threading support. This “strange” default behaviour is for performance reasons, no shame in that.

http://uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html