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中设置了守护进行,如上注释部分所示

  • 相关阅读:
    HDU5120
    POJ 1062
    POJ 1086
    BestCoder 1st Anniversary (HDU 5311)
    HDU 5284
    Dylans loves sequence(hdu5273)
    day65 作业
    第三次小组分享 猴子补丁
    day59 csrf auth
    day58 cookie session 中间件
  • 原文地址:https://www.cnblogs.com/cjj-zyj/p/9930550.html
Copyright © 2011-2022 走看看