Python:为方法调用计时,如果超过时间,则停止该方法调用 [英] Python: time a method call and stop it if time is exceeded

查看:1156
本文介绍了Python:为方法调用计时,如果超过时间,则停止该方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态加载代码(作为源代码),运行它并获取结果.我加载的代码始终包含一个run方法,该方法返回所需的结果.因为我可以做到,所以一切看起来都像Python中的平常一样简单,

I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do

exec(source) #source includes run() definition
result = run(params)
#do stuff with result

唯一的问题是,动态生成的代码中的run()方法可能无法终止,因此我只需要运行x秒钟即可.我可以为此生成一个新线程,并为.join()方法指定一个时间,但是随后我无法轻易地从中获取结果(或者可以).性能也是要考虑的问题,因为所有这些都是在很长的循环中发生的

The only problem is, the run() method in the dynamically generated code can potentially not terminate, so I need to only run it for up to x seconds. I could spawn a new thread for this, and specify a time for .join() method, but then I cannot easily get the result out of it (or can I). Performance is also an issue to consider, since all of this is happening in a long while loop

关于如何进行的任何建议?

Any suggestions on how to proceed?

根据dcrosta的请求清除内容:所加载的代码不是不受信任的,而是在计算机上自动生成的.这样做的目的是基因编程.

to clear things up per dcrosta's request: the loaded code is not untrusted, but generated automatically on the machine. The purpose for this is genetic programming.

推荐答案

唯一的真正好的"解决方案(基本上没有开销)将直接或通过漂亮的抽象层基于SIGALRM;但是如前所述,Windows不支持此功能.线程是没有用的,不是因为很难获得结果(使用队列,这是微不足道的!),而是因为以一种很好的跨平台方式强行终止一个失控的线程是不可行的.

The only "really good" solutions -- imposing essentially no overhead -- are going to be based on SIGALRM, either directly or through a nice abstraction layer; but as already remarked Windows does not support this. Threads are no use, not because it's hard to get results out (that would be trivial, with a Queue!), but because forcibly terminating a runaway thread in a nice cross-platform way is unfeasible.

这使高开销的multiprocessing成为唯一可行的跨平台解决方案.您将需要一个进程池来减少进程产生的开销(因为大概只是偶尔需要杀死一个失控的函数,在大多数情况下,您可以通过向其发送新函数来执行来重用一个已有的进程).再次,Queue(多处理类型)使返回结果变得容易(尽管在多线程情况下要谨慎一点,因为在多处理情况下可能会发生死锁).

This leaves high-overhead multiprocessing as the only viable cross-platform solution. You'll want a process pool to reduce process-spawning overhead (since presumably the need to kill a runaway function is only occasional, most of the time you'll be able to reuse an existing process by sending it new functions to execute). Again, Queue (the multiprocessing kind) makes getting results back easy (albeit with a modicum more caution than for the threading case, since in the multiprocessing case deadlocks are possible).

如果您不需要严格地序列化函数的执行,而可以安排您的体系结构以并行尝试其中的两个或多个,并且可以在多核计算机(或快速运行的多台计算机)上运行LAN),然后突然间多处理成为一种高性能解决方案,可以轻松地偿还产生的费用和IPC开销以及更多,这恰恰是因为您可以利用尽可能多的处理器(或群集中的节点).

If you don't need to strictly serialize the executions of your functions, but rather can arrange your architecture to try two or more of them in parallel, AND are running on a multi-core machine (or multiple machines on a fast LAN), then suddenly multiprocessing becomes a high-performance solution, easily paying back for the spawning and IPC overhead and more, exactly because you can exploit as many processors (or nodes in a cluster) as you can use.

这篇关于Python:为方法调用计时,如果超过时间,则停止该方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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