python程序 執行緒

2021-08-28 23:11:48 字數 3838 閱讀 5922

程序:

單個程序:

from multiprocessing import process

import time

def task(msg):

print ('hello, %s' % msg)

time.sleep(1)

if __name__ == '__main__':

p = process(target=task, args=('world',))

p.start()

if p.is_alive():

print ('process: %s is running' % p.pid)

p.join()

多程序

from multiprocessing import process

import time

def task(msg):

print ('hello, %s' % msg)

time.sleep(1)

if __name__ == '__main__':

for i in range(5):

p = process(target=task, args=('world',))

p.start()

if p.is_alive():

print ('process: %s is running' % p.pid)

p.join()

使用程序池,程序在執行時最多不能超過程序池的大小

from multiprocessing import pool

import os,time

def run_task(name):

print('task',os.getpid())

time.sleep(2)

print('task',os.getpid(),' is end')

if __name__=='__main__':

p=pool(processes=3)

for i in range(5):

p.close()

p.join()

print('all is done')

程序之間的通訊:

queue:多程序之間通訊:

from multiprocessing import process,queue 

import os,time,random

def proc_write(q,urls):

print('process (%s) is writing' % os.getpid())

for url in urls:

q.put(url)

print('put %s to queue' % url)

time.sleep(random.random())

def proc_read(q):

print('process (%s) is reading' % os.getpid())

while true:

url=q.get(true)

print('get %s from queue' % url)

if __name__=='__main__':

q=queue()

proc_write1=process(target=proc_write,args=(q,['url_1','url_2','url_3']))

proc_write2=process(target=proc_write,args=(q,['url_4','url_5','url_6']))

proc_reader=process(target=proc_read,args=(q,))

proc_write1.start()

proc_write2.start()

proc_reader.start()

proc_write1.join()

proc_write2.join()

proc_reader.terminate()

pipe:管道,兩個程序之間的通訊,乙個send,乙個revc

import os,time,random,multiprocessing

def proc_send(pipe,urls):

for url in urls:

print('process (%s) is sending %s.' % (os.getpid(),url))

pipe.send(url)

time.sleep(random.random())

def proc_read(pipe):

while true:

try:

msg=pipe.recv()

print('process (%s) rev %s.' % (os.getpid(),msg))

except eoferror:

break

if __name__=='__main__':

pipe=multiprocessing.pipe()

p1=multiprocessing.process(target=proc_send,args=(pipe[0],['url_'+str(i) for i in range(10)]))

p2=multiprocessing.process(target=proc_read,args=(pipe[1],))

p1.start()

p2.start()

pipe[0].close()

pipe[1].close()

p1.join()

p2.join()

執行緒:

多執行緒

import time,threading

def music(name):

time.sleep(3)

def movie(name):

time.sleep(3)

threads=

t1=threading.thread(target=music,args=(u'沙漠駱駝',))

t2=threading.thread(target=movie,args=(u'影',))

if __name__=='__main__':

for t in threads:

t.setdaemon(true)

t.start()

t.join()

print('time is',time.time())

lock同步

import time,threading

lock=threading.lock()

v=1000

def b(n):

lock.acquire()

try:

global v

v+=n

print(threading.current_thread(),'is running.v:',v)

finally:

lock.release()

t1=threading.thread(target=b,args=(6,))

t2=threading.thread(target=b,args=(8,))

t3=threading.thread(target=b,args=(8,))

t1.start()

t2.start()

t3.start()

t1.join()

t2.join()

t3.join()

print(v)

python 執行緒vs程序

使用執行緒的兩種方式 函式或用類包裝執行緒物件 函式 式 呼叫thread模組中是start new thread 函式產生新執行緒 thread.start new thread function,args kwargs function 執行緒函式。args 傳遞給執行緒函式的引數,他必須是個t...

python程序 執行緒通訊

main程序下,建立多個執行緒,同一程序下執行緒可以共享位址空間,全域性變數 同一程序下執行緒共享那些資源 usr bin env python coding utf 8 import random import time from multiprocessing import queue,proc...

python執行緒與程序

直接呼叫 import threading,time def run n print running n time.sleep 2 t1 threading.thread target run,args test1 生成乙個執行緒例項 t2 threading.thread target run,a...