单任务 按顺序执行
# *_*coding:utf-8 *_* import time def sing(): for i in range(5): print("i am singing"); time.sleep(1) def dance(): for i in range(5): print('i am danceing') time.sleep(1) def main(): sing() dance() if __name__ == "__main__": main()
执行结果如下
(python37) mac@macdeMacBook-Pro:/www/learn_lumen/python$ python test.py i am singing i am singing i am singing i am singing i am singing i am danceing i am danceing i am danceing i am danceing i am danceing
改为多线程
# *_*coding:utf-8 *_* import time import threading def sing(): for i in range(5): print("唱山歌咯"+ str(i)); time.sleep(1) def dance(): for i in range(5): print('跳舞'+str(i)) time.sleep(1) def main(): t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() print("main进程") if __name__ == "__main__": main()
执行结果
(python37) [root@localhost python]# python test.py 唱山歌咯0 跳舞0 main进程 唱山歌咯1 跳舞1 唱山歌咯2 跳舞2 唱山歌咯3 跳舞3 唱山歌咯4 跳舞4