代码拉取完成,页面将自动刷新
同步操作将从 fslong/udseqq 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 多进程、多线程、异步io学习 '
__author__ = 'fslong'
__version__ = '0.0.1'
import asyncio
import multiprocessing
import threading, time, queue
def job(j):
print('子线程/子进程:%s' % (j + 1))
print('线程数量:', threading.active_count())
#print('线程:', threading.current_thread())
for i in range(1000000):
j += i
#time.sleep(3)
return j
def job1(j, q):
print('子线程/子进程:%s' % (j + 1))
print('线程数量:', threading.active_count())
#print('线程:', threading.current_thread())
for i in range(1000000):
j += i
#time.sleep(3)
q.put(j)
return j
async def yibu(j, result):
print('子线程/子进程:%s' % (j + 1))
print('线程数量:', threading.active_count())
#print('线程:', threading.current_thread())
for i in range(1000000):
j += i
result.append(j)
def mp():
pool = multiprocessing.Pool()
result = []
for i in range(100):
res = pool.apply_async(job, args=(i, ))
result.append(res.get())
pool.close()
pool.join()
return result
def loop():
q = queue.Queue()
result = []
threads = []
for i in range(100):
t = threading.Thread(
target=job1, args=(
i,
q,
))
t.start()
threads.append(t)
for i in threads:
i.join()
result.append(q.get())
return result
def yibuIo():
result = []
loop = asyncio.get_event_loop()
task = [yibu(i, result) for i in range(100)]
loop.run_until_complete(asyncio.wait(task))
return result
if __name__ == '__main__':
start1 = time.time()
i = 0
while i < 10:
i += 1
result = []
for j in range(100):
result.append(job(j))
#print(result)
end1 = time.time()
start2 = time.time()
i = 0
while i < 10:
i += 1
mp()
#print(mp())
end2 = time.time()
start3 = time.time()
i = 0
while i < 10:
i += 1
loop()
#print(loop())
end3 = time.time()
start4 = time.time()
i = 0
while i < 10:
i += 1
yibuIo()
#print(yibuIo())
end4 = time.time()
print('采用单线程执行了10次耗时:%s' % (end1 - start1))
print('采用多线程执行了10次耗时:%s' % (end3 - start3))
print('采用多进程执行了10次耗时:%s' % (end2 - start2))
print('采用异步IO执行了10次耗时:%s' % (end4 - start4))
#input()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。