Pause a thread/ execfile()

Sean DiZazzo half.italian at gmail.com
Mon Oct 26 04:01:52 EDT 2009


On Oct 25, 11:58 pm, Babloo <pruthviraj... at gmail.com> wrote:
> i have a small python application with GUI (frontend) which has
> various functions. I have a "RUN" button which runs python scripts in
> the background . It basically calls execfile() function internally
> which runs in a thread ,  to run the python script .
>
> I want to implement a "PAUSE" feature which would pause the running
> python script . So do that i have to either pause the thread in which
> execfile() runs or pause execfile itself .
>
> When the user presses "RUN" again then the python script should resume
> running .
>
> Any ideas how to pause the thread / pause execfile() . Any other ideas
> for the same would be helpful .
>
> Thanks

I think you can do that with a threading.event().  In the gui, create
a new threading.event() and pass it in to the worker thread.  Set up
your code in the worker so that on every iteration of "work", it
checks to see if the event is set.  If it finds it set, then it
sleeps, but keeps checking until the event is unset.  When unset
again, it picks up and begins work again.

In the gui, your pause button just sets and unsets the event, and the
worker will find out and pause at the next iteration.

Depending on what kind of work your worker thread is doing, it might
be tough to structure the code so the event gets checked reasonably
often.  Hope this helps.

~Sean

PS.  Not sure this idea will hold up when using execfile()



More information about the Python-list mailing list