python 多进程,多线程简单的

python 多进程,多线程简单的

# -*- coding:utf-8 -*-
from time import ctime,sleep
import threading #线程
import multiprocessing #多进程

def talk(c,loop):
    for i in range(loop):
        print(" %s talk hello 51 zxw! %s" %(c,ctime()))
        sleep(3)
        
def write(c,loop):
    for i in range(loop):
        print(" %s wite hello 51 zxw! %s" %(c,ctime()))
        sleep(5)

threads = []
# t1 = threading.Thread(target=talk,args=("t1",2)) #多线程用这个
t1 = multiprocessing.Process(target=talk,args=("t1",2)) #多进程用这个
threads.append(t1)

# t2 = threading.Thread(target=write,args=("t1",2))
t2 = multiprocessing.Process(target=write,args=("t1",2))
threads.append(t2)

if __name__ == "__main__":
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print("all the end %s" %ctime())
原文地址:https://www.cnblogs.com/zhenyu1/p/14696915.html