pnpnpn / timeout-decorator

Timeout decorator for Python
MIT License
627 stars 94 forks source link

Fails on generators #44

Open ColCarroll opened 6 years ago

ColCarroll commented 6 years ago

Hi! This is a useful project, but appears to fail on generators. Namely, this function:

import time
from timeout_decorator import timeout

@timeout(seconds=0.5)
def sleeper(s):
    for j in range(10):
        time.sleep(s)
        yield j

runs fine regardless of the value of s: try list(sleeper(0.1)), or list(sleeper(1)), for example.

Also, setting use_signals=False, makes everything fail after seconds:

@timeout(seconds=0.5, use_signals=False)
def sleeper(s):
    for j in range(10):
        time.sleep(s)
        yield j

For example, list(sleeper(0.01)), or even just sleeper(0) both throw an error after 0.5s. Indeed,

for j in sleeper(0.1):
    print(j)

does not print anything before failing after 0.5s.

renero commented 3 years ago

Same here. The decorator works fine under "normal" conditions, but fail with decorators.