python request timeout_Python - aiohttp请求不断超时(Python - aiohttp requests continuously time out)...

Python - aiohttp请求不断超时(Python - aiohttp requests continuously time out)

我有一个Python程序,它使用aiohttp和ElementTree从网站获取数据。 下面的代码是Raspberry Pi上托管的Discord聊天机器人的一部分。 该功能在大多数情况下运行良好,但是在机器人开启几天之后,该功能开始停滞并且总是超时。 重新启动程序并不能解决问题,只有重启Pi才能解决问题。 我知道这并不是很多,但是这段代码有什么明显的问题可以解决这个问题,或者问题出在别的什么地方?

import lxml.etree as ET

import aiohttp, async_timeout

...

async with aiohttp.ClientSession() as session:

try:

with async_timeout.timeout(5):

async with session.get('https://example.com', params=params, headers=headers) as resp:

if resp.status == 200:

root = ET.fromstring(await resp.text(), ET.HTMLParser())

# Do stuff with root

else:

print("Error: {}".format(resp.response))

except Exception as e:

print("Timeout error {}".format(e))

I have a Python program that uses aiohttp and ElementTree to fetch data from a website. The code below is a segment of a Discord chat bot hosted on a Raspberry Pi. The function works well most of the time, but after the bot has been on for a few days, the function begins to bog down, and always times out. Restarting the program doesn't fix the issue, only rebooting the Pi seems to solve the problem for a while. I know it's not a lot to go on, but is there an obvious issue with this segment of code that could case this, or does the problem lie somewhere else?

import lxml.etree as ET

import aiohttp, async_timeout

...

async with aiohttp.ClientSession() as session:

try:

with async_timeout.timeout(5):

async with session.get('https://example.com', params=params, headers=headers) as resp:

if resp.status == 200:

root = ET.fromstring(await resp.text(), ET.HTMLParser())

# Do stuff with root

else:

print("Error: {}".format(resp.response))

except Exception as e:

print("Timeout error {}".format(e))

原文:https://stackoverflow.com/questions/47215072

更新时间:2020-02-04 11:44

最满意答案

也许内存泄漏缓慢地占用了系统内存,一旦所有内容都变得非常缓慢,因为交换被用于内存分配并发生超时。

然而,正如Andrew所说,这不能成为python脚本的问题,或者通过重新启动它来解决。

监视系统内存并从那里开始。

Perhaps a memory leak somewhere that slowly uses up systems memory, once full everything becomes very slow as swap is used for memory allocation and timeouts occur.

However as Andrew says this can't be a problem with the python script or it would be fixed by restarting it.

Monitor system memory and go from there.

2017-11-12

相关问答

response.headers是一个常规属性,在调用之前无需等待 另一方面, asyncio.wait接受期货和退货(done, pending)对的列表。 看起来你应该用await asyncio.gather(*tasks)替换await wait()调用( 收集doc ) response.headers is a regular property, no need to put await before the call asyncio.wait on other hand accept

...

代理是错的。 不同的代理导致不同的错误,因此很难找到一个好的代理。 上面的代码绝对有效(但请更改代理!)。 The proxy was wrong. Different proxies cause different errors, so it was hard to find one fine proxy. The code above is absolutely valid (but pls change the proxy!).

变量d包含对字典的引用(“指针”)。 text.append(d)语句只是将相同字典的引用添加到列表中。 因此,在N次迭代之后,您对列表中的d具有N个相同的引用。 如果你改变你循环成这样的东西: for ip in ip_list:

d["ip"]=ip

text.append(d)

print(text)

你应该在控制台上看到: [{'ip': '192.168.1.1'}]

[{'ip': '18.9.8.1'}, {'ip': '18.9.8.1'}]

[{'ip'

...

你的测试技术出了点问题。 针对您的服务器运行wrk工具会提供不同的结果。 要运行的命令: wrk http://127.0.0.1:15000/

服务器输出: ======== Running on http://0.0.0.0:15000 ========

(Press CTRL+C to quit)

2016-10-23 14:58:56,447 - webserver - INFO - Request id: hkkrp received - will sleep for 10

201

...

您正在等待单独的do_request()调用。 而不是直接等待它们(在协程完成之前阻塞它们),使用asyncio.gather()函数让事件循环同时运行它们: async def main():

create_database_and_tables()

records = prep_sample_data()[:100]

requests = []

for record in records:

r = Record(record)

...

当使用AsyncResolver作为连接的解析器时,我遇到了类似的问题。 它曾经是默认的解析器,所以它可能是你的情况。 该问题与ipv6的域有关,其中AsyncResolver存在问题,因此解决方案是简单地将族指定为ipv4地址 conn = aiohttp.TCPConnector(

family=socket.AF_INET,

verify_ssl=False,

)

I had a similar issue when using AsyncResol

...

也许内存泄漏缓慢地占用了系统内存,一旦所有内容都变得非常缓慢,因为交换被用于内存分配并发生超时。 然而,正如Andrew所说,这不能成为python脚本的问题,或者通过重新启动它来解决。 监视系统内存并从那里开始。 Perhaps a memory leak somewhere that slowly uses up systems memory, once full everything becomes very slow as swap is used for memory allocatio

...

一般来说,每当运行事件循环时,您应尽量避免使用线程。 不幸的是, rethinkdb不支持asyncio开箱即用,但它确实支持Tornado和Twisted框架。 因此,您可以桥接 Tornado和asyncio ,并在不使用线程的情况下使其工作。 编辑 : 正如安德鲁所指出的, rethinkdb 确实支持asyncio 。 在2.1.0之后你可以做到: rethinkdb.set_loop_type("asyncio")

然后在您的Web处理程序中: res = await rethinkd

...

将密钥传递给fetch() ,以使用相应的响应返回它们: #!/usr/bin/env python

import asyncio

import aiohttp # $ pip install aiohttp

async def fetch(session, key, item, base_url='http://example.com/posts/'):

async with session.get(base_url + item) as response:

retu

...

我也在学习它。 我发现了这个问题https://github.com/hangoutsbot/hangoutsbot/pull/655 。 那么代码就像这样 @asyncio.coroutine

def _create_server(self):

app = web.Application(loop=self.loop)

return app

def add_handler(self, url, handler):

self.app.router.add_route('G

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值