zoukankan      html  css  js  c++  java
  • 多任务 执行

    单任务 按顺序执行

    # *_*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
    

      

  • 相关阅读:
    rabbitmq安装详解
    linux下安装rabbitmq的rpm包问题记录
    在 CentOS 6.4上安装Erlang
    redis配置认证密码(转)
    Redis单台的安装部署及主备、哨兵部署
    查看linux系统版本的命令
    Js 实现ajax
    json的相关操作
    Diango思维图
    服务系统 server端
  • 原文地址:https://www.cnblogs.com/php-linux/p/12771522.html
Copyright © 2011-2022 走看看