8

I have Thread and I want to do change some flags when it finish. For example:

import threading 

def on_done():
    # do some printing or change some flags

def worker():
    # do some calculations

t = threading.Thread(target=worker, on_close=on_done)
t.start()

But i was unable to find method like that. Any ideas how could I manage it somehow? The worker function is in separet file, so it does not know abou the flags.

Thaks in advance

Sony Nguyen
  • 103
  • 1
  • 1
  • 4
  • 1
    Remember that Python runs under the GIL (Global Interpreter Lock). so, the threads runs concurrent but not parallel. This means that you don't need nothing special to change the value of a flag, just change it from the same scope that you call worker, calling on_done after that. – Hamlett May 08 '16 at 22:49
  • [GIL](https://wiki.python.org/moin/GlobalInterpreterLock) – Hamlett May 08 '16 at 22:57

1 Answers1

10

One option is to simply wrap the worker function:

def wrapped_worker():
    worker()
    on_done()

Then, set the target to wrapped_worker:

t = threading.Thread(target=wrapped_worker, on_close=on_done)

See this answer for a more in-depth example.

Community
  • 1
  • 1
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94