• python进程、线程


    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    import _thread
    import logging
    from time import ctime,sleep
    logging.basicConfig(level=logging.INFO)
    def loop0():
        logging.info("start loop0 at " + ctime())
        sleep(4)
        logging.info("ent loop0 at " + ctime())
    
    def loop1():
        logging.info("start loop1 at " + ctime())
        sleep(2)
        logging.info("end loop1 at " + ctime())
    
    def main():
        logging.info("start all at " + ctime())
        # 同时进行
        _thread.start_new_thread(loop0,())
        _thread.start_new_thread(loop1,())
        # 如果不延时6秒 main这个线程就结束了,会把thread里的进程kill掉
        sleep(6)
        logging.info("end all at " + ctime())
    # 在当前模块
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    import _thread
    import logging
    from time import ctime,sleep
    logging.basicConfig(level=logging.INFO)
    
    timer = [4,6]
    def loop(index,nsec,lock):
        print(nsec)
        logging.info('start loop' + str(index) + ' at' + ctime())
        sleep(nsec)
        logging.info('end loop' + str(index) + ' at' + ctime())
        lock.release()
    
    def main():
        logging.info('start all at ' + ctime())
        locks = []
        length = range(len(timer))
        print(length)
        for i in length:
            lock = _thread.allocate_lock()
            lock.acquire()
            locks.append(lock)
        for i in length:
            _thread.start_new_thread(loop,(i,timer[i],locks[i]))
        # 判断两个锁有被锁过 才执行下一行
        for i in length:
            while locks[i].locked():pass
        logging.info('end all at ' + ctime())
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    在这里插入图片描述

    # #threading
    import logging
    import threading
    from time import ctime,sleep
    logging.basicConfig(level=logging.INFO)
    
    timer = [4,6]
    def loop(index,nsec):
        logging.info('start loop' + str(index) + ' at' + ctime())
        sleep(nsec)
        logging.info('end loop' + str(index) + ' at' + ctime())
    
    def main():
        logging.info('start all at ' + ctime())
        threads = []
        length = range(len(timer))
        for i in length:
            t = threading.Thread(target=loop,args=(i,timer[i]))
            threads.append(t)
        for i in length:
            threads[i].start()
        for i in length:
            threads[i].join()
        logging.info('end all at ' + ctime())
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述
    在这里插入图片描述

    # #threading改写
    import logging
    import threading
    from time import ctime,sleep
    logging.basicConfig(level=logging.INFO)
    
    timer = [4,6]
    class MyThread(threading.Thread):
        def __init__(self,fun,args,name=''):
            threading.Thread.__init__(self)
            self.fun = fun
            self.args = args
            self.name = name
        def run(self):
            print(self.name)
            self.fun(*self.args)
    
    def loop(index,nsec):
        logging.info('start loop' + str(index) + ' at' + ctime())
        sleep(nsec)
        logging.info('end loop' + str(index) + ' at' + ctime())
    
    def main():
        logging.info('start all at ' + ctime())
        threads = []
        length = range(len(timer))
        for i in length:
            t = MyThread(loop, (i,timer[i]), loop.__name__+str(i))
            threads.append(t)
        for i in length:
            threads[i].start()
        for i in length:
            threads[i].join()
        logging.info('end all at ' + ctime())
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    【物联网】windows环境 配置mqtt服务器
    linux——(5 部分软件安装)
    LeetCode --- 1971. Find if Path Exists in Graph 解题报告
    springboot+vue+nodejs的校园短期闲置资源置换系统java
    使用IDEA时遇到java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错的解决方案
    japi项目需求分析阶段
    你真的懂反馈吗?
    java计算机毕业设计废品回收管理系统设计与实现源码+mysql数据库+系统+lw文档+部署
    【数据结构】线性表(四)双向链表的各种操作(插入、删除、查找、修改、遍历打印)
    英伟达AI布局的新动向:H200 GPU开启生成式AI的新纪元
  • 原文地址:https://blog.csdn.net/camille009/article/details/126628459