zoukankan      html  css  js  c++  java
  • python标准库介绍——33 thread 模块详解

    ?==thread 模块==
    
    
    (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 
    只有你在编译解释器时打开了线程支持才可以使用它. 如果没有特殊需要, 最好使用高级接口 ``threading`` 模块替代. 
    
    ====Example 3-6. 使用 thread 模块====[eg-3-6]
    
    ```
    File: thread-example-1.py
    
    import thread
    import time, random
    
    def worker():
        for i in range(50):
            # pretend we're doing something that takes 10?00 ms
            time.sleep(random.randint(10, 100) / 1000.0)
            print thread.get_ident(), "-- task", i, "finished"
    
    #
    # try it out!
    
    for i in range(2):
        thread.start_new_thread(worker, ())
    
    time.sleep(1)
    
    print "goodbye!"
    
    *B*311 -- task 0 finished
    265 -- task 0 finished
    265 -- task 1 finished
    311 -- task 1 finished
    ...
    265 -- task 17 finished
    311 -- task 13 finished
    265 -- task 18 finished
    goodbye!*b*
    ```
    
    注意当主程序退出的时候, 所有的线程也随着退出. 而 ``threading`` 模块不存在这个问题 . (该行为可改变)
    

      

  • 相关阅读:
    12
    11
    10
    9
    8
    6. iOS APP 设计规范大全
    4. iOS中常用演示方法以及利弊
    我要写一篇动态计算tableView-cell高度的随笔
    doclever 5.5.1 安装及升级【原创】
    SPARROW-JS 从0开始写 0依赖,原生JS框架
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/7784778.html
Copyright © 2011-2022 走看看