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()
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)