本文介绍了使用多处理功能在Python中创建超时功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多处理库在Python 2.7.11(在Windows上)中创建超时功能.

I'm trying to create a timeout function in Python 2.7.11 (on Windows) with the multiprocessing library.

我的基本目标是,如果函数超时,则返回一个值,如果不超时,则返回实际值.

My basic goal is to return one value if the function times out and the actual value if it doesn't timeout.

我的方法如下:

from multiprocessing import Process, Manager

def timeoutFunction(puzzleFileName, timeLimit):
  manager = Manager()
  returnVal = manager.list()

  # Create worker function
  def solveProblem(return_val):
    return_val[:] = doSomeWork(puzzleFileName) # doSomeWork() returns list

  p = Process(target=solveProblem, args=[returnVal])
  p.start()

  p.join(timeLimit)
  if p.is_alive():
    p.terminate()
    returnVal = ['Timeout']

  return returnVal

我这样调用函数:

if __name__ == '__main__':
  print timeoutFunction('example.txt', 600)

不幸的是,这不起作用,我在pickle.py中收到EOF错误

Unfortunately this doesn't work and I receive some sort of EOF error in pickle.py

任何人都可以看到我在做什么吗?

Can anyone see what I'm doing wrong?

在此先感谢,
亚历山大

Thanks in advance,
Alexander

编辑:doSomeWork()不是实际功能.只是我做的其他一些工作的补充.该工作不是并行完成的,并且不使用任何共享变量.我只是尝试运行一个函数,并使其可能超时.

doSomeWork() is not an actual function. Just a filler for some other work I do. That work is not done in parallel and does not use any shared variables. I'm only trying to run a single function and have it possibly timeout.

推荐答案

您可以使用 Pebble 图书馆.

from pebble import concurrent
from concurrent.futures import TimeoutError

TIMEOUT_IN_SECONDS = 10

@concurrent.process(timeout=TIMEOUT_IN_SECONDS)
def function(foo, bar=0):
    return foo + bar

future = function(1, bar=2)

try:
    result = future.result()  # blocks until results are ready or timeout
except TimeoutError as error:
    print "Function took longer than %d seconds" % error.args[1]
    result = 'timeout'

文档包含更完整的示例.

如果该库超时,它将终止该函数,因此您不必担心IO或CPU的浪费.

The library will terminate the function if it timeouts so you don't need to worry about IO or CPU being wasted.

如果您正在做作业,您仍然可以查看实现.

If you're doing an assignment, you can still look at its implementation.

简短示例:

from multiprocessing import Pipe, Process

def worker(pipe, function, args, kwargs):
    try:
        results = function(*args, **kwargs)
    except Exception as error:
        results = error

    pipe.send(results)

pipe = Pipe(duplex=False)
process = Process(target=worker, args=(pipe, function, args, kwargs))

if pipe.poll(timeout=5):
    process.terminate()
    process.join()
    results = 'timeout'
else:
    results = pipe.recv()

Pebble提供了一个简洁的API,可以处理极端情况并使用更可靠的机制.然而,这或多或少是它在幕后所做的.

Pebble provides a neat API, takes care of corner cases and uses more robust mechanisms. Yet this is more or less what it does under the hood.

这篇关于使用多处理功能在Python中创建超时功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:50