zoukankan      html  css  js  c++  java
  • 论多线程python2与python3

    python3 常用线程

    # -*- coding: utf-8 -*-
    import time
    from threading import Thread
    
    
    def test(i):
        while True:
            print("i",i+1)
            time.sleep(1)
            i+=1
            if i == 5:
                break
    print(1)
    t = Thread(target=test, args=(0,))
    #
    t.setDaemon(True)
    t.start()
    print("---",2)

    主线程一直运行,遇到循环耗时操作分出子线程,主线程运行到最后等待子线程结束,再进行关闭

    python2.7 thread方法

    # -*- coding: utf-8 -*-
    import time
    # from threading import Thread
    import thread
    
    def test(i):
        while True:
            print "i",i+1
            time.sleep(1)
            i+=1
            if i == 5:
                break
    print 1
    thread.start_new_thread(test, (0,))
    # t = Thread(target=test, args=(0,))
    # t.start()
    print "---",2

    主线程运行到最后就结束,相当于python3中设置了守护进行,如上注释部分所示

  • 相关阅读:
    JS和Flash相互调用
    xml的应用
    随机验证码
    模块 time
    第一天 注册成功
    我的第一篇博客
    git
    2018-02-27
    25
    建站之星
  • 原文地址:https://www.cnblogs.com/cjj-zyj/p/9930550.html
Copyright © 2011-2022 走看看