如何处理请求.exception.ConnectionError in multiprocess python.



在发布我的问题之前,我已经阅读了很多文章,因为它们都没有回答它,或者它对于我的 Python 知识来说太复杂了。我正在尝试通过多处理发送几个帖子请求,我注意到当我的 Raspberry Pi 没有成功连接到网络时,我有一些 ConnectionError: ('Connection Aborted.', gaierror(-2, 'Name or service not known')

所以我在我的代码中这样做:

url = "https://some_url.org}"
list.append(multiprocessing.Process(target=requests.post, args=(url,)))
#doing this several times with several urls, then
for element in list:
    element.start()
for element in list:
    element.join()

我知道我必须做from requests.exceptions import ConnectionError,我已经在 ̀ for 循环中尝试了一个简单的try: except:,但它不起作用,因为异常位于子进程中。以下是回溯:

Process Process-6:
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
    self.run()
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send

提前感谢任何帮助!

问题:如何处理请求。异常。连接多处理中的错误

requests.post(url)调用使用wrapper,例如:

import multiprocessing as mp
import requests
def req_wrapper(url):
    try:
        requests.post(url)
    except Exception as e:
        print('Exception catched in req_wrapper:{}'.format(e))
if __name__ == '__main__':
    url = "https://some_url.org"
    p = mp.Process(target=req_wrapper, args=(url,))
    p.start()
    p.join()

输出
在 req_wrapper 中捕获异常:("连接中止",gaierror(-2,"名称或服务未知"((

使用 Python 测试:3.4.2 和 2.7.9

相关内容

  • 没有找到相关文章

最新更新