1. 程式人生 > >Python腳本之Multiprocessing 多線程

Python腳本之Multiprocessing 多線程

python多線程


######################################

#!/usr/bin/env python

#encoding=utf-8

from multiprocessing import Process,Lock

import time,os

def say(i):

print ‘nihao:‘,i

#lock=Lock()

for n in range(101):

p=Process(target=say,args=(n,))

p.start()

#p.join()

########################################多線程池Pool,可限制多線程數量

#!/usr/bin/env python

#encoding=utf-8

import multiprocessing

import os,sys,time

result=[]

def run_cmd(i):

print ‘threading test:‘,i,os.getpid()

time.sleep(1.5)

p=multiprocessing.Pool(processs=25)

for n in range(100):

result.append(p.apply_async(run_cmd,(‘%s‘%n,)))

p.close()

#p.join()

for res in result:

res.get(timeout=5)

######################################

Python腳本之Multiprocessing 多線程