python 多线程

 1 import multiprocessing
 2 import time,threading
 3 
 4 def threading_run():
 5     print(threading.get_ident())#获取线程ID
 6 
 7 def run(name):
 8     time.sleep(2)
 9     print('hello', name)
10     t = threading.Thread(target=threading_run,)#每个进程下创建一个线程
11     t.start()
12 
13 if __name__ == '__main__':
14     for i in range(10):#10个线程
15         p = multiprocessing.Process(target=run, args=('bob%s'%i,))
16         p.start()
17     # p.join()

获取进程ID:

 1 from multiprocessing import Process
 2 import os
 3 
 4 
 5 def info(title):
 6     print(title)
 7     print('module name:', __name__)
 8     print('parent process:', os.getppid())#获取父进程ID
 9     print('process id:', os.getpid())#获取自己进程ID
10     print("

")
11 
12 
13 def f(name):
14     info('33[31;1mcalled from child process function f33[0m')
15     print('hello', name)
16 
17 
18 if __name__ == '__main__':
19     info('33[32;1mmain process line33[0m')
20     p = Process(target=f, args=('bob',))
21     p.start()
22     # p.join()
View Code
原文地址:https://www.cnblogs.com/anhao-world/p/13702605.html