码迷,mamicode.com
首页 > 编程语言 > 详细

线程,线程池|进程,进程池

时间:2017-04-16 18:04:20      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:bin   not   false   submit   cut   tor   notify   join()   gif   

线程

import threading

 

 

lock=threading.Lock() #

lock=threading.RLock() #递归锁,多层锁定,多层解锁

lock.acquire()

lock.release()

技术分享
import threading
import time
v = 10
#lock = threading.Lock() # 只能开一把
lock = threading.RLock()# 可以开多把

def task(arg):
    time.sleep(2)
    # 申请使用锁,其他人等
    lock.acquire()
    lock.acquire()
    global v
    v -= 1
    print(v)
    # 释放
    lock.release()
    lock.release()

for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()
View Code

 


多个人使用锁

lock = threading.BoundedSemaphore(3)
lock.acquire()

lock.release()

技术分享
import threading
import time
lock = threading.BoundedSemaphore(3)

def task(arg):
    # 申请使用锁,其他人等
    lock.acquire()
    time.sleep(1)
    global v
    v -= 1
    print(v)
    # 释放
    lock.release()

for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()
View Code

 




事件(锁,所有锁的释放与锁定)

lock = threading.Event()

lock.wait()


lock.clear()

技术分享
import threading
import time
lock = threading.Event()

def task(arg):
    time.sleep(1)
    # 锁住所有的线程
    lock.wait()
    # 申请使用锁,其他人等
    print(arg)


for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()
while True:
    value = input(>>>>)
    if value == 1:
        lock.set()
        lock.clear()
View Code

 


指定释放几个线程锁

lock = threading.Condition()

 


技术分享
import threading
import time
lock = threading.Condition()

def task(arg):
    time.sleep(1)
    # 锁住所有的线程
    lock.acquire()
    lock.wait()
    # 申请使用锁,其他人等
    print(线程,arg)
    lock.release()


for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()
while True:
    value = input(>>>>)
    lock.acquire()
    lock.notify(int(value))  #指定释放几个锁
    lock.release()
View Code

线程池

技术分享
from concurrent.futures import ThreadPoolExecutor  #导入指定模块
import time
import requests

def task(url):
    response=requests.get(url)
    print("请求结果",url,len(response.content))

url_list=[
    "http://www.baidu.com",
    "http://cn.bing.com",
    "http://www.oldboyedu.com"
]
pool=ThreadPoolExecutor(2)   #创建线程池
for url in url_list:
    pool.submit(task,url)        #线程池调用
View Code

 

自定义回调函数

技术分享
from concurrent.futures import ThreadPoolExecutor
import time
import requests

def txt(future):
    cc=future.result()
    print(cc)
def task(url):
    response=requests.get(url)
    return(url,len(response.content))

url_list=[
    "http://www.baidu.com",
    "http://cn.bing.com",
    "http://www.oldboyedu.com"
]
pool=ThreadPoolExecutor(2)
for url in url_list:
    future=pool.submit(task,url)
    future.add_done_callback(txt)
View Code

 

进程
from multiprocessing import Process #导入模块

技术分享
from multiprocessing import Process
import time
def task(arg):
    time.sleep(arg)
    print(arg)

if __name__ == __main__:
    for i in range(10):
        p = Process(target=task,args=(i,))
        p.daemon = True
        # p.daemon = False
        p.start()
        p.join(1)

    print(主进程最后...)
View Code

 

进程间内存共享

1、Array

技术分享
from multiprocessing import Process,Array
import time
import threading

def task(num,li):
    li[num]=1
    print(list(li))

if __name__ == __main__:
    v = Array("i",10)
    for i in range(10):
        p = Process(target=task,args=(i,v,))
        # p = threading.Thread(target=task,args=(i,v,))
        p.start()
View Code

 

2、Manager (内部socket对接连接)

Manager.list() Manager.dict()

 

技术分享
from multiprocessing import Process,Manager
import time
import threading

def task(num,li):
    li.append(num)
    print(list(li))

if __name__ == __main__:
    dic=Manager().list()
    for i in range(10):
        p = Process(target=task,args=(i,dic,))
        # p = threading.Thread(target=task,args=(i,v,))
        p.start()
        p.join()
View Code

进程池

 

技术分享
from concurrent.futures import ProcessPoolExecutor

def call(arg):
    data = arg.result()
    print(data)

def task(arg):
    print(arg)
    return arg + 100

if __name__ == __main__:
    pool = ProcessPoolExecutor(5)
    for i in range(10):
        obj = pool.submit(task,i)
        obj.add_done_callback(call)
View Code

 

 

 

 

 










 
 

 

线程,线程池|进程,进程池

标签:bin   not   false   submit   cut   tor   notify   join()   gif   

原文地址:http://www.cnblogs.com/arthas-zht/p/6718650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!