python--多线程的应用

Posted 小陆同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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--多线程的应用的主要内容,如果未能解决你的问题,请参考以下文章

[Python3] 043 多线程 简介

python中的多线程和多进程编程

什么是多线程,多进程?

多线程 Thread 线程同步 synchronized

多线程编程

Java多线程与并发库高级应用-工具类介绍