相关疑难解决方法(0)

具有超时,最大大小和连接池的http请求

我正在寻找Python(2.7)中的一种方法来执行具有3个要求的HTTP请求:

  • 超时(可靠性)
  • 内容最大尺寸(安全性)
  • 连接池(用于性能)

我已经检查了所有python HTTP库,但它们都不符合我的要求.例如:

urllib2:很好,但没有汇集

import urllib2
import json

r = urllib2.urlopen('https://github.com/timeline.json', timeout=5)
content = r.read(100+1)
if len(content) > 100: 
    print 'too large'
    r.close()
else:
    print json.loads(content)

r = urllib2.urlopen('https://github.com/timeline.json', timeout=5)
content = r.read(100000+1)
if len(content) > 100000: 
    print 'too large'
    r.close()
else:
    print json.loads(content)
Run Code Online (Sandbox Code Playgroud)

请求:没有最大尺寸

import requests
r = requests.get('https://github.com/timeline.json', timeout=5, stream=True)
r.headers['content-length'] # does not exists for this request, and not safe
content = r.raw.read(100000+1)
print content # ARF this is gzipped, so not the real …
Run Code Online (Sandbox Code Playgroud)

python timeout connection-pooling http max-size

8
推荐指数
1
解决办法
1万
查看次数

自定义调度程序具有带超时/终止开关的顺序+半连续脚本?

下面是我的代码的一个重要部分,基本上如果你向下滚动到该execute_subscripts()功能,你可以看到我有两个脚本运行,通过execfile哪个工作很漂亮,他们表明prints,他们将traceback错误保存到错误文件.

我试图将第二个脚本变成一个不等待自己完成的脚本,然后再转到下一个脚本.

正如你所看到的,我试图用subprocessPopen来启动一个无声的隐藏窗口......但它似乎没有运行,我不知道如何p.communicate()正确使用该函数来检索tracebacks和/或prints.

我也...需要帮助创建某种超时/终止开关,所以如果一个下标要么通过Popenexecfile路由没有在5分钟内完成它跳过它为该循环或重试并跳过如果它立即再次失败.

我明白我可能不应该strftime在时间使用....但是这部分对我来说很好,所以我认为不需要改变它.

from datetime import date, timedelta
from sched import scheduler
from time import time, sleep, strftime
import random
import traceback
import subprocess

s = scheduler(time, sleep)
random.seed()

def periodically(runtime, intsmall, intlarge, function):

     ## Get current time
    currenttime = strftime('%H:%M:%S')

    ## If currenttime is anywhere between 23:40 and 23:50 then...
    if currenttime …
Run Code Online (Sandbox Code Playgroud)

python subprocess scheduler execfile python-2.7

7
推荐指数
2
解决办法
588
查看次数

等待特定时间的未来

您将如何等待特定时间的未来响应?

比如说,我们在关闭http请求之前发出一个http post请求并等待它的响应,但是,我们只等待3秒,否则我们关闭请求.

你会如何实现这一目标?

就像是

Future makePostReq() async{
  .... 

  await http response for 3 secs

  .... 

 if(response) {
  ... Do something with it
 }

 Http.close

} 
Run Code Online (Sandbox Code Playgroud)

asynchronous future async-await dart flutter

7
推荐指数
3
解决办法
1934
查看次数

如果函数耗时太长,跳过循环?

我的 Python 代码花费的时间太长,如果花费的时间超过几秒钟,我想停止并跳过此函数的执行。

比如我要计时的函数是:

batch_xs, batch_ys = train_loadbatch_from_lists(batch_size)
Run Code Online (Sandbox Code Playgroud)

在某些情况下,这个函数调用花费的时间太长,我想取消它。

我正在寻找这样的东西:

if time for batch_xs, batch_ys = train_loadbatch_from_lists(batch_size) > 20 seconds:
    then skip
Run Code Online (Sandbox Code Playgroud)

参考这篇文章

我想知道如果发生超时,我将如何再次调用该函数。

例如

@timeout(15)
def abcd(hello):
#some def 
Run Code Online (Sandbox Code Playgroud)

如果它超过计时器,我想再次调用该函数。

python numpy

6
推荐指数
1
解决办法
1万
查看次数

DNS超时在“请求” python库上

对于我的项目,我必须检查网站的状态(在共享主机上)。

我使用Python请求库。

def getStatusCode(url):
    try:
        return requests.head(url,timeout=0.3).status_code
    except:
        return -1
Run Code Online (Sandbox Code Playgroud)

此代码在MacOS 10.10和Python3.4(带有类似http://www.google.com的网址)下的效果很好。如果拔下ISP电缆,则会立即出现异常。

在具有Python3.4的Ubuntu Server 14.04下,如果拔下ISP电缆,则永远不会收到超时错误。Raspbian上的同样问题。

经过一些测试,如果我用IP http://216.58.212.100替换了URL ,Ubuntu Server会给我一个例外,但是由于我位于共享的Web托管上,所以我不能使用IP。

经过一番研究,我发现请求库中的超时与DNS查找之间没有区别,不是由它执行而是由操作系统执行。

所以我的问题是解决这个问题的最美丽的方法是什么?我是否需要在Python中添加额外的超时异常,例如:函数调用超时

谢谢

python dns python-requests

5
推荐指数
1
解决办法
2081
查看次数

如何在 Python 中运行后台计时器

我目前正在制作一个数学游戏,用户有 60 秒的时间来回答尽可能多的问题。到目前为止,除了计时器应该倒计时到 0 或计数到 60 然后停止游戏之外,我一切正常。现在,我将计时器设置为 time.clock() 以计数到 60,当计时器小于该值时,游戏将继续运行。然而,出于某种原因, time.clock() 并没有像我期望的那样工作。我还尝试同时运行两个 while 循环,但也没有用。任何人都可以帮助我吗?只是寻找一种在后台运行计时器的方法。

这是我的代码:

    score = 0
    timer = time.clock()
    lives = 3

    while timer < 60 and lives > 0:
        if score >= 25:
            x = random.randint(-100,100)
            y = random.randint(-100,100)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 20:
            x = random.randint(-75,75)
            y = random.randint(-75,75)
            answer …
Run Code Online (Sandbox Code Playgroud)

python time timer while-loop

3
推荐指数
1
解决办法
2万
查看次数

可嵌套超时装饰器?(即超时装饰函数调用超时装饰函数)

我需要一个装饰器(或功能等效的东西)来允许下面的代码按预期工作:

@timeout(1)
def outer():
    inner()

@timeout(5)
def inner():
    time.sleep(3)
    print("Should never be printed if you call outer()")

outer()
# The outer timeout is ignored and "property" finishes
Run Code Online (Sandbox Code Playgroud)

该代码看起来毫无意义,但实际上,outer调用了多个函数,这些函数花费了不确定的时间,其中一些函数有自己的超时时间。

我在这里尝试了timeout-decorator两个 SO 答案,但没有一个有效。

python timeout decorator

3
推荐指数
1
解决办法
420
查看次数

Pytest 断言超时?

有没有办法断言 Pytest 测试用例由于 pytest 超时而失败?我想运行一个寿命测试,希望运行不会出现问题,直到遇到 pytest 超时。我用 @pytest.mark.timeout(6000) 注释测试以覆盖默认的 pytest 超时,当遇到 6000 秒超时时,测试失败并显示E Failed: Timeout >6000.0s.

我尝试添加with pytest.raises(pytest.TimeoutExpired)到我的测试中以捕获最终的超时,但这似乎并不能解决问题。有没有办法正确捕获 pytest 引发的超时?

python pytest

3
推荐指数
1
解决办法
3599
查看次数