进程和线程

Posted dorami

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进程和线程相关的知识,希望对你有一定的参考价值。

线程池

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)

 

以上是关于进程和线程的主要内容,如果未能解决你的问题,请参考以下文章

进程和线程单线程多线程和多进程

进程和线程,线程安全,python如何实现多进程,多线程

进程与线程的问题

多线程和多进程模式有啥区别

什么是并发和并行?什么是进程和线程?进程和线程的区别

进程和线程的区别和联系