Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble Nesting stopit #17

Open
SourdoughCat opened this issue Mar 31, 2018 · 3 comments · May be fixed by #36
Open

Trouble Nesting stopit #17

SourdoughCat opened this issue Mar 31, 2018 · 3 comments · May be fixed by #36

Comments

@SourdoughCat
Copy link

SourdoughCat commented Mar 31, 2018

Hi,

I'm having a bit of trouble nesting timeouts. Platforms tested:

  • Windows 10 Pro
  • Mac OSX

My shortest reproducible solution is here:

from stopit import ThreadingTimeout as Timeout
import time

def print_stuff(idx):
    while True:
        time.sleep(1)
        print(idx)


def test_this():
    t0 = time.time()
    for idx in range(5):
        with Timeout(5) as timeout_inner:
            print_stuff(idx)
    return time.time() - t0

with Timeout(10) as timeout_outer:
    res = test_this()

assert res < 15

Where the output is:

0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-28-abee5adecac5> in <module>()
     18     res = test_this()
     19 
---> 20 assert res < 15

AssertionError: 
@glenfant
Copy link
Owner

glenfant commented Apr 4, 2018

Please read the README section about the timeout accuracy issue when using thread blocking funcs like time.sleep(xxx) within a Timeout context manager.

@SourdoughCat
Copy link
Author

SourdoughCat commented Apr 5, 2018

I don't believe this is a timing issue.

My point is that nesting threading blocks literally do not work. Could you provide a pattern or an example where it does work?

I could replace the for loop with an infinite loop and it would never terminate.

As an example this would never terminate

from stopit import ThreadingTimeout as Timeout
import time

def print_stuff(idx):
    while True:
        time.sleep(1)
        print(idx)


def test_this():
    t0 = time.time()
    while True:
        with Timeout(5) as timeout_inner:
            print_stuff(1)
    return time.time() - t0

with Timeout(10) as timeout_outer:
    res = test_this()

@lpyparmentier
Copy link

lpyparmentier commented Nov 19, 2020

Hi, I had the same issue, and it's related with the fact that the ThreadingTimeout set a timer on the same thread_id (https://github.com/glenfant/stopit/blob/master/src/stopit/threadstop.py#L45). Only way to solve this is to set-up different thread, or find a way to raise different exception per context manager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants