zoukankan      html  css  js  c++  java
  • python--多线程的应用

    python 多线程执行函数,以及调用函数时传参

    import threading
    
    def func1():
        print('this is function1')
    
    
    def func2(x,y):
        print('this is function2')
        print(x+y)
    
    threads = []
    threads.append(threading.Thread(target=func1))
    threads.append(threading.Thread(target=func2,args=(11,22,)))
    print(threads)
    if __name__ == '__main__':
        for t in threads:
            print(t)
            t.start()
    View Code

      

    python 多线程执行cmd终端命令(os.system),

    # import threading
    # import time
    # class ThreadImpl(threading.Thread):
    #     def __init__(self, num,cmds):
    #         threading.Thread.__init__(self)
    #         self._num = num
    #         self._cmds = cmds
    #
    #     def run(self):
    #         global total, mutex
    #
    #         # 打印线程名
    #
    #         print(threading.currentThread().getName())
    #         os.system(self._cmds)
    #         for x in range(0, int(self._num)):
    #             # 取得锁
    #             mutex.acquire()
    #             # total = total + 1
    #             # 释放锁
    #             mutex.release()
    #
    #
    # if __name__ == '__main__':
    #     starttime = time.time()
    #     global  mutex
    #     mutex = threading.Lock()
    #     threads = []
    #     for x in range(len(cmdList)):
    #         threads.append(ThreadImpl(100,cmdList[x]))
    #     # 启动线程
    #     for t in threads:
    #         t.start()
    #     # 等待子线程结束
    #     for t in threads:
    #         t.join()
    #
    #     totaltime = time.time() - starttime
    #     # print('========')
    #     # print(totaltime)
    View Code

      

  • 相关阅读:
    Python多进程实现并行化随机森林
    Python多进程队列间传递对象
    Umlet和draw.io 使用心得
    简单认识Adam优化器
    使用BERT进行情感分类预测及代码实例
    【深度学习】深入理解Batch Normalization批标准化
    Vue插件总结
    Vue配置环境识别
    pc端微信上传BUG
    Vue原生订单列表
  • 原文地址:https://www.cnblogs.com/lutt/p/12303405.html
Copyright © 2011-2022 走看看