2

我正在开发一个 PyQt 程序,它基本上从互联网上收集数据。在此示例中,我尝试从 RSS 网页获取数据。

假设 self.feed 是 RSS 页面,包含所有文章,假设“条目”是一篇文章。“entry.url”是网站上一篇文章的原始页面。

from requests_futures.sessions import FuturesSession

self.session_pages = FuturesSession(max_workers=20)
for entry in self.feed.entries:
    future = self.session_pages.get(entry.url, timeout=10)
    future.add_done_callback(my_call_back)

这基本上就是我的做法。它嵌入在 PyQt 线程中,我同时运行多个线程,但我认为问题不在于 PyQt。

我的问题是我认为期货不会关闭连接,即使它们完成了。我这样检查:

lsof -i | grep "python" | wc -l

losf -i 给出连接中涉及的打开文件。该命令的其余部分是计算打开的文件数。这个数字不会停止增长(类似于 900),然后我收到以下错误:

(python:28285): GLib-ERROR **: Creating pipes for GWakeup: Too many open files
[1]    28285 trace trap (core dumped)  python gui.py

我认为问题来自期货,但实际上我不确定。

我试过类似的东西:

self.session_pages.shutdown()

在线程的末尾,但它没有工作。

你有什么主意吗 ?

4

2 回答 2

2

好的,你是 rigt @jm_____。get() 调用只是对 requests.get 的调用。所以我使用了这里的答案:

Python-请求关闭 http 连接

更具体地说:

future = self.session_pages.get(url, timeout=20, headers={'Connection':'close'})

现在 lsof 表示正常数字。谢谢你。

于 2015-04-19T20:56:23.577 回答
2

我没有FutureSession在 python 中看到concurrent.futures?我在这里做一些假设。

除非回调对每个回调都是唯一的,否则self.session_page.get(...)我认为该future.add_done_callback(my_call_back)行可能正在创建新的,并覆盖回调的对象 ID,或者可能不正确?

这是我在您使用的上下文中唯一可以找到参考的地方:FutureSession

from pprint import pprint
from requests_futures.sessions import FuturesSession

session = FuturesSession()

def bg_cb(sess, resp):
    # parse the json storing the result on the response object
    resp.data = resp.json()

future = session.get('http://httpbin.org/get', background_callback=bg_cb)
# do some other stuff, send some more requests while this one works
response = future.result()
print('response status {0}'.format(response.status_code))
# data will have been attached to the response object in the background
pprint(response.data)

尝试设置background_callback

更新:

我会尝试使用self.session_pages.request而不是self.session_pages.get因为FutureSession由线程池执行器和requests.Sessions

是的,情况就是这样:

(Pdb) inspect.getmro(FuturesSession)
(<class '__main__.FuturesSession'>, <class 'requests.sessions.Session'>, <class 'requests.sessions.SessionRedirectMixin'>, <class 'object'>)
(Pdb) vars()
{'DEFAULT_POOLSIZE': 10, '__return__': None, '__spec__': None, 'inspect': <module 'inspect' from '/usr/lib/python3.4/inspect.py'>, '__file__': 'requestsfutures.py', 'FuturesSession': <class '__main__.FuturesSession'>, 'HTTPAdapter': <class 'requests.adapters.HTTPAdapter'>, 'ThreadPoolExecutor': <class 'concurrent.futures.thread.ThreadPoolExecutor'>, 'Session': <class 'requests.sessions.Session'>, '__name__': '__main__', '__cached__': None, '__doc__': "\nrequests_futures\n~~~~~~~~~~~~~~~~\n\nThis module provides a small add-on for the requests http library. It makes use\nof python 3.3's concurrent.futures or the futures backport for previous\nreleases of python.\n\n    from requests_futures import FuturesSession\n\n    session = FuturesSession()\n    # request is run in the background\n    future = session.get('http://httpbin.org/get')\n    # ... do other stuff ...\n    # wait for the request to complete, if it hasn't already\n    response = future.result()\n    print('response status: {0}'.format(response.status_code))\n    print(response.content)\n\n", 'pdb': <module 'pdb' from '/usr/lib/python3.4/pdb.py'>, '__loader__': <_frozen_importlib.SourceFileLoader object at 0x7f6d84194470>, '__builtins__': <module 'builtins' (built-in)>, '__package__': None}
(Pdb) vars().keys()
dict_keys(['DEFAULT_POOLSIZE', '__return__', '__spec__', 'inspect', '__file__', 'FuturesSession', 'HTTPAdapter', 'ThreadPoolExecutor', 'Session', '__name__', '__cached__', '__doc__', 'pdb', '__loader__', '__builtins__', '__package__'])
(Pdb) vars()['FuturesSession']
<class '__main__.FuturesSession'>
(Pdb) vars()['FuturesSession'].get
<function Session.get at 0x7f6d80c07488>
(Pdb) vars()['Session'].get
<function Session.get at 0x7f6d80c07488>
于 2015-04-19T20:03:43.717 回答