0
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:

  1. 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)
  2. 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.

Menglong Li
  • 2,177
  • 14
  • 19
  • 1
    "Why the thread numbers in Task and the Main are same?": you're possibly using an OS that supports forking. – Ilja Everilä Dec 20 '17 at 07:36
  • @Ilja Everilä, thx, I searched some info like "Fork is nothing but a new process that looks exactly like the old or the parent process but still it is a different process with different process ID and having it’s own memory." So it makes sense, if so how we distinguish different threads as they looks similar, using PID? – Menglong Li Dec 20 '17 at 07:49
  • Btw why do you need to identify the process/thread? To your 2nd item: ["Added callables are called in the order that they were added and are always called in a thread belonging to the process that added them."](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future.add_done_callback) I've a hard time thinking why calling the callback in the worker process when the future is done would be useful, but that's just my lack of imagination. – Ilja Everilä Dec 20 '17 at 09:53

0 Answers0