import _thread
import time
import logging
logging.basicConfig(level=logging.INFO)defloop0():
logging.info("start loop0 at "+ time.ctime())
time.sleep(4)
logging.info("end loop0 at "+ time.ctime())defloop1():
logging.info("start loop1 at "+ time.ctime())
time.sleep(2)
logging.info("end loop1 at "+ time.ctime())defmain():
logging.info("start all at "+ time.ctime())
loop0()
loop1()
logging.info("end all at "+ time.ctime())defmain():
logging.info("start all at "+ time.ctime())
_thread.start_new_thread(loop0,())
_thread.start_new_thread(loop1,())# _thread主线程退出后,所有的子线程会被quit掉,没有守护线程
time.sleep(6)
logging.info("end all at "+ time.ctime())if __name__ =='__main__':
main()
INFO:root:start all at Sat Nov 716:31:202020
INFO:root:start loop0 at Sat Nov 716:31:202020
INFO:root:start loop1 at Sat Nov 716:31:202020
INFO:root:end loop1 at Sat Nov 716:31:222020
INFO:root:end loop0 at Sat Nov 716:31:242020
INFO:root:end all at Sat Nov 716:31:262020
_thread-第二种实现方法
import _thread
import time
import logging
logging.basicConfig(level=logging.INFO)# 利用锁
loops =[2,4]defloop(nloop, nsec, lock):
logging.info(f"start loop{nloop} at {time.ctime()}")
time.sleep(nsec)
logging.info(f"end loop{nloop} at {time.ctime()}")
lock.release()defmain():
logging.info("start all at "+ time.ctime())
locks =[]
nloops =range(len(loops))for i in nloops:
lock = _thread.allocate_lock()
lock.acquire()
locks.append(lock)for i in nloops:
_thread.start_new_thread(loop,(i, loops[i], locks[i]))for i in nloops:while locks[i].locked():pass
logging.info("end all at "+ time.ctime())if __name__ =='__main__':
main()
INFO:root:start all at Sat Nov 716:36:402020
INFO:root:start loop0 at Sat Nov 716:36:402020
INFO:root:start loop1 at Sat Nov 716:36:402020
INFO:root:end loop0 at Sat Nov 716:36:422020
INFO:root:end loop1 at Sat Nov 716:36:442020
INFO:root:end all at Sat Nov 716:36:442020
threading-第一种实现
import threading
import time
import logging
logging.basicConfig(level=logging.INFO)
loops =[2,4]defloop(nloop, nsec):
logging.info(f"start loop{nloop} at {time.ctime()}")
time.sleep(nsec)
logging.info(f"end loop{nloop} at {time.ctime()}")defmain():
logging.info("start all at "+ time.ctime())
threads =[]
nloops =range(len(loops))for i in nloops:
t = threading.Thread(target=loop, args=(i,loops[i]))
threads.append(t)for i in nloops:
threads[i].start()for i in nloops:
threads[i].join()
logging.info("end all at "+ time.ctime())if __name__ =='__main__':
main()
INFO:root:start all at Sat Nov 716:38:542020
INFO:root:start loop0 at Sat Nov 716:38:542020
INFO:root:start loop1 at Sat Nov 716:38:542020
INFO:root:end loop0 at Sat Nov 716:38:562020
INFO:root:end loop1 at Sat Nov 716:38:582020
INFO:root:end all at Sat Nov 716:38:582020
threading-第二种实现
import threading
import time
import logging
logging.basicConfig(level=logging.INFO)
loops =[2,4]classMyThread(threading.Thread):def__init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.func = func
self.args = args
self.name = name
defrun(self):
self.func(*self.args)defloop(nloop, nsec):
logging.info(f"start loop{nloop} at {time.ctime()}")
time.sleep(nsec)
logging.info(f"end loop{nloop} at {time.ctime()}")defmain():
logging.info("start all at "+ time.ctime())
threads =[]
nloops =range(len(loops))for i in nloops:
t = MyThread(loop,(i, loops[i]), loop.__name__)
threads.append(t)for i in nloops:
threads[i].start()for i in nloops:
threads[i].join()
logging.info("end all at "+ time.ctime())if __name__ =='__main__':
main()
INFO:root:start all at Sat Nov 716:40:202020
INFO:root:start loop0 at Sat Nov 716:40:202020
INFO:root:start loop1 at Sat Nov 716:40:202020
INFO:root:end loop0 at Sat Nov 716:40:222020
INFO:root:end loop1 at Sat Nov 716:40:242020
INFO:root:end all at Sat Nov 716:40:242020