python 多进程与多线程
python 多进程与多线程
#utf-8
from multiprocessing import Process
from os import getpid
from time import time, sleep
def download_task(testname):
while (True):
print("%s--%s\n"%(testname,getpid()))
def main1(): #多进程
start = time()
p1 = Process(target=download_task, args=('test1', ))
p1.start()
p2 = Process(target=download_task, args=('test2', ))
p2.start()
p1.join()
p2.join()
end = time()
print('总共耗费了%.2f秒.' % (end - start))
from random import randint
from threading import Thread
from time import time, sleep
class DownloadTask(Thread):
def __init__(self, filename):
super().__init__()
self._filename = filename
def run(self):
while (True):
print("%s--%s\n" %(self._filename,getpid()))
def main2():#多线程
start = time()
t1 = DownloadTask('test1') # t1 = Thread(target=download, args=('test1',))
t1.start()
t2 = DownloadTask('test2')
t2.start()
t1.join()
t2.join()
end = time()
print('总共耗费了%.2f秒.' % (end - start))
if __name__ == '__main__':
#main2() #多线程
main1() #多进程
。
#utf-8
from multiprocessing import Process
from os import getpid
from time import time, sleep
def download_task(testname):
while (True):
print("%s--%s\n"%(testname,getpid()))
def main1(): #多进程
start = time()
p1 = Process(target=download_task, args=('test1', ))
p1.start()
p2 = Process(target=download_task, args=('test2', ))
p2.start()
p1.join()
p2.join()
end = time()
print('总共耗费了%.2f秒.' % (end - start))
from random import randint
from threading import Thread
from time import time, sleep
class DownloadTask(Thread):
def __init__(self, filename):
super().__init__()
self._filename = filename
def run(self):
while (True):
print("%s--%s\n" %(self._filename,getpid()))
def main2():#多线程
start = time()
t1 = DownloadTask('test1') # t1 = Thread(target=download, args=('test1',))
t1.start()
t2 = DownloadTask('test2')
t2.start()
t1.join()
t2.join()
end = time()
print('总共耗费了%.2f秒.' % (end - start))
if __name__ == '__main__':
#main2() #多线程
main1() #多进程
很久没有写博客了,感觉只要是不写博客,人就很变得很懒,学的知识点感觉还是记不住,渐渐地让我明白,看的越多,懂的越少(你这话不是有毛病吗?应该是看的越多,懂的越多才对),此话怎讲,当你在茫茫的前端知识库里面东看看,西看看的时候,很快就被海量的知识给淹没了,根本就不知道哪些是对的,哪些是错的,感觉好像这个也懂了,那个也懂了,但是真正写起来,脑子又一片空白,又好像什么都不懂,这种状态时有发生,这就叫不懂装懂,最根本的原因就是看的太多,写的太少,所以为了改掉这样毛病,把被动学习变成主动学习,接下来的日子,多写写,即使是写一些学习工作中遇到的坑也是好的,没事翻出来看看,还可以加深印象,好了,废话到处!