bwangel23
9/24/2016 - 2:23 PM

查看 add_done_callback 函数的运行线程

查看 add_done_callback 函数的运行线程

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import threading
import sys
import time

from concurrent.futures import ThreadPoolExecutor


executor = ThreadPoolExecutor()

def wait_on_future():
    print("[{}]: thread id is {}".format(sys._getframe(0).f_code.co_name,
                                         threading.current_thread().ident))
    time.sleep(5)
    return pow(5, 22)


def get_thread_id(future):
    print("[{}]: thread id is {}".format(sys._getframe(0).f_code.co_name,
                                         threading.current_thread().ident))


def main():
    print("[{}]: thread id is {}".format(sys._getframe(0).f_code.co_name,
                                         threading.current_thread().ident))
    f = executor.submit(wait_on_future)
    f.add_done_callback(get_thread_id)

if __name__ == '__main__':
    main()