超时(...): [英] with timeout(...):

查看:86
本文介绍了超时(...):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人写过一个上下文执行超时的上下文管理器?
python2.5?


我很想能写出像这样的内容/>
超过超时(5.0)的


some_long_running_stuff()

如果超出:

print" ;哎呀 - 花了太长时间!


让它以可靠的方式工作并以跨平台的方式运行!


来自我的超时实验我怀疑它不可能在$ 2.5中完美地实现它 - 也许我们可以为Python 3k添加一些额外的

核心基础设施以使其成为可能吗? br />

-

Nick Craig-Wood< ni ** @ craig-wood.com-- http://www.craig-wood.com/nick

解决方案

Nick Craig-Wood写道:


是否有人编写了一个实现超时的contextmanager

python2.5?


我希望能够用超时(5.0)写出类似


的东西超出:

some_long_running_stuff()

如果超出:

print"哎呀 - 花了太长时间!"

让它以可靠的方式工作并以跨平台的方式工作!


从我的超时实验中我怀疑它不可能是

在python 2.5中完美实现它 - 也许我们可以为Python 3k添加一些额外的

核心基础设施以使其成为可能?



I 我猜你的问题远远超过我的想法,但是如果我理解的话,

我会采取刺:


首先,你有没有想要超时杀死长时间运行的东西吗?


我不确定它是不是你要找的,但我写了一个

计时器做类似你描述的类:

http://aspn.activestate.com/ASPN/Coo.../Recipe/464959


通过将

适当的函数作为expire传递,你可以在超时时做任何你想做的事情。参数。


这类似于屏幕保护程序等。


James


Nick Craig-Wood写道:


是否有人编写了一个实现超时的上下文管理器

python2.5?


我希望能够写出类似


超时(5.0)的东西:

some_long_running_stuff()<如果超过


打印哎呀 - 花了太长时间!


让它在十字架上可靠地工作平台方式!



跨平台在这里不是问题 - 可靠性虽然如此。说得好

简单:不可能这样做。你当然可以在

python字节码核心中添加一个计时器,它将跳回。到存储的保存点或

之类的东西。


但为了使工作可靠,必须确保没有副作用
$ b在some_long_running_stuff中出现$ b。它不仅仅扩展到了
python本身,还包括外部模块和系统(文件编写,网络

通信......)。除非你使用时间机器,否则不可能做到这一点。

这是我个人的侮辱,因为在那个回滚期限内

我可能会向未来的妻子提出建议......


Diez


3月26日下午3:16 ,Diez B. Roggisch, < d ... @nospam.web.dewrote:


但为了使工作可靠,必须确保没有副作用
$ b在some_long_running_stuff中出现$ b。它不仅仅扩展到了
python本身,还包括外部模块和系统(文件编写,网络

通信......)。除非你使用时间机器,否则无法做到这一点。



嘿,嘿,我们都不是大人的咒语吗?程序员有责任只使用没有

副作用的代码。

我当然可以确保

代码中没有出现副作用:1 + 2。我甚至不需要时间机器来执行此操作:P

或原语可以实现,以便Python

尽早抛出TimeoutException。然后一个

可以写出除了块之外的任何不受欢迎的
副作用。 (我不是说这样的超时功能可能是在
Python中实现的
,但可以通过修改CPython实现来实现)


Did anyone write a contextmanager implementing a timeout for
python2.5?

I''d love to be able to write something like

with timeout(5.0) as exceeded:
some_long_running_stuff()
if exceeded:
print "Oops - took too long!"

And have it work reliably and in a cross platform way!

From my experiments with timeouts I suspect it won''t be possible to
implement it perfectly in python 2.5 - maybe we could add some extra
core infrastructure to Python 3k to make it possible?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick

解决方案

Nick Craig-Wood wrote:

Did anyone write a contextmanager implementing a timeout for
python2.5?

I''d love to be able to write something like

with timeout(5.0) as exceeded:
some_long_running_stuff()
if exceeded:
print "Oops - took too long!"

And have it work reliably and in a cross platform way!

From my experiments with timeouts I suspect it won''t be possible to
implement it perfectly in python 2.5 - maybe we could add some extra
core infrastructure to Python 3k to make it possible?

I''m guessing your question is far over my head, but if I understand it,
I''ll take a stab:

First, did you want the timeout to kill the long running stuff?

I''m not sure if its exactly what you are looking for, but I wrote a
timer class that does something like you describe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/464959

Probably you can do whatever you want upon timeout by passing the
appropriate function as the "expire" argument.

This works like a screen saver, etc.

James


Nick Craig-Wood wrote:

Did anyone write a contextmanager implementing a timeout for
python2.5?

I''d love to be able to write something like

with timeout(5.0) as exceeded:
some_long_running_stuff()
if exceeded:
print "Oops - took too long!"

And have it work reliably and in a cross platform way!

Cross platform isn''t the issue here - reliability though is. To put it
simple: can''t be done that way. You could of course add a timer to the
python bytecode core, that would "jump back" to a stored savepoint or
something like that.

But to make that work reliably, it has to be ensured that no sideeffects
occur while being in some_long_running_stuff. which doesn''t only extend to
python itself, but also external modules and systems (file writing, network
communications...). Which can''t be done, unless you use a time-machine.
Which I''d take as an personal insult, because in that rolled-back timeframe
I will be possibly proposing to my future wife or something...

Diez


On Mar 26, 3:16 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:

But to make that work reliably, it has to be ensured that no sideeffects
occur while being in some_long_running_stuff. which doesn''t only extend to
python itself, but also external modules and systems (file writing, network
communications...). Which can''t be done, unless you use a time-machine.

Hey hey, isn''t the Python mantra that we''re all adults here? It''d
be the programmers responsibility to use only code that has no
side effects. I certainly can ensure that no side-effects occur in the
following code: 1+2. I didn''t even need a time machine to do that :P
Or the primitive could be implemented so that Python
throws a TimeoutException at the earliest opportunity. Then one
could write except-blocks which deal with rolling back any undesirable
side effects. (I''m not saying such timeout feature could be
implemented in
Python, but it could be made by modifying the CPython implementation)


这篇关于超时(...):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆