函数 threading.active_count() threading.current_thread() threading.get_ident() threading.enumerate() threading.main_thread() threading.settrace(func) threading.setprofile(func) threading.stack_size([size]) threading.TIMEOUT_MAX

threading模块一共提供的类包括:

函数 threading.local

例子如下:

threading class MyThread1(threading.Thread): def run(self): local = threading.local() if 'name' not in local.__dict__: print('thread1 not set name') local.name = 'li' print('thread1 {}'.format(local.name)) class MyThread2(threading.Thread): def run(self): local = threading.local() if 'name' not in local.__dict__: print('thread2 not set name') local.name = 'wang' print('thread2 {}'.format(local.name)) def main(): print("Start main threading") local = threading.local() local.name = 'main' threads = [MyThread1(), MyThread2()] for t in threads: t.start() # 一次让新创建的线程执行 join for t in threads: t.join() print('main {}'.format(local.name)) if __name__ == '__main__': main()

相关文章: