from concurrent.futures import ProcessPoolExecutor
import threading
import os
def task():
print('Task: in thread', threading.current_thread())
print('Task: in process', os.getpid())
def taskDone(fn):
print('Callback: in thread', threading.current_thread())
print('Callback: in process', os.getpid())
def main():
print('Main: in thread', threading.current_thread())
print('Main: in process', os.getpid())
with ProcessPoolExecutor(max_workers=3) as executor:
future = executor.submit(task)
future.add_done_callback(taskDone)
if __name__ == '__main__':
main()
The codes are like above, and the output looks like:
Main: in thread <_MainThread(MainThread, started 140737137116096)>
Main: in process 80462
Task: in thread <_MainThread(MainThread, started 140737137116096)>
Task: in process 80463
Callback: in thread <Thread(Thread-1, started daemon 123145465724928)>
Callback: in process 80462
Questions:
- Why the thread numbers in Task and the Main are same? So we need to use process id together with thread to distinguish the thread? (Seems that thread id alone is not enough)
- Is it possible to make the callback function happen under the same process as Task, so that they can share something? For this case, I just made it as simple as possible. But was it possible?
Answers and discussions to any of these two questions are welcome.