954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to tell if a frame has expired

I made a simple ThreadManager class that receives tasks and spawns threads to complete the tasks. Currently it keeps all the threads alive until I explicitly set threadmgr.waiting=False and all of the tasks are complete. I'm trying to eliminate the need for the former. What I would prefer is that when the frame that instantiates the threadmgr terminates it automatically tells the threadmgr to stop waiting for new tasks. I've been looking at the inspect module and am able to get a line number that the parent frame is on but I don't see an easy way to determine if that frame has completed... It's probably a fairly simple solution. Thanks in advance.

ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

The normal solution is to use a 'with' statement

from contextlib import contextmanager

@contextmanager
def threadmgr_running(*args, **kwd):
    threadmgr = ThreadManager(*args, **kwd)
    threadmgr.start() # or whatever
    try:
        yield threadmgr
    finally:
        threadmgr.waiting = False

# in code

with threadmgr_running(...) as threadmgr:
    etc # the threadmgr terminates automatically after this block
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

thanks, that gave me the idea to just add __enter__ and __exit__ methods to the class and use it within a with statment.

ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: