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

进程和线程

时间:2019-08-15 19:25:21      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:col   hashlib   pid   with open   join()   利用   content   start   process   

线程池

import threadpool
import threading
import requests
import hashlib

def down_load_file(url):
    r = requests.get(url)
    m = hashlib.md5(url.encode())
    print(正在下载====%s%m.hexdigest())
    with open(%s.jpg%m.hexdigest(),wb) as fw:
        fw.write(r.content)


url_list = [http://www.nnzhp.cn/wp-content/uploads/2019/02/jiami.jpeg,
            http://www.nnzhp.cn/wp-content/uploads/2019/03/js.png,
            http://www.nnzhp.cn/wp-content/uploads/2018/08/ab389f04cb5b57344ef9655428bccaec.png
            ]
pool = threadpool.ThreadPool(10)#创建一个线程池,指定线程池最多有多少个线程

reqs = threadpool.makeRequests(down_load_file,url_list) #调用生成启动线程用的参数

# for req in reqs:
#     pool.putRequest(req)

[pool.putRequest(req) for req in reqs]

print(当前的线程数,threading.activeCount())

pool.wait() #等待

print(测试结束)

守护线程

import time,threading,os
import threadpool

count = 0
lock = threading.Lock() #申请一把锁

def lajfenlei():
    global count

    # lock.acquire()
    # count += 1
    # lock.release()
    with lock:
        count+=1
    print(干垃圾)

for i in range(10):
    syy = threading.Thread(target=lajfenlei,)
    # syy.setDaemon(True) #把子线程设置成守护线程
    syy.start()
while threading.activeCount() != 1:
    pass

print(count)
print(完成!)


#8
#python GIL 全局解释器锁,导致python的多线程利用不了多核cpu,但是多进程可以。

#

多进程

import multiprocessing
import time
import threading

def say():
    time.sleep(2)
    print(hhhh)

def lajfenlei():
    for i in range(10):
        t = threading.Thread(target=say)
        t.start()
    print(threading.activeCount())
    print(垃圾分类)

if __name__ == __main__:
    for i in range(5):
        p = multiprocessing.Process(target=lajfenlei)
        p.start()
        print(p.pid )

    while len(multiprocessing.active_children())!=0:#等待子进程执行完成
        pass

    print(子进程都运行完了。)

多线程

import threading
import requests,time

def lajfenlei():
    time.sleep(2)
    print(干垃圾)

# start_time = time.time()
# for i in range(10):
#     lajfenlei()
# print( time.time() - start_time)

def shangke():
    print(上课)

start_time = time.time()


#第一种方法,主线程等待子线程
# threads =[]
#
# for i in range(10):
#     syy = threading.Thread(target=lajfenlei,)
#     syy.start()
#     threads.append(syy)
#
# for t in threads:
#     t.join()

for i in range(10):
    syy = threading.Thread(target=lajfenlei,)
    syy.start()

while True:
    if threading.activeCount()==1:
        break

end_time = time.time()

print(end_time - start_time)

 

进程和线程

标签:col   hashlib   pid   with open   join()   利用   content   start   process   

原文地址:https://www.cnblogs.com/Dorami/p/11359447.html

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