日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python线程、进程、进程池、协程

發(fā)布時間:2024/2/28 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python线程、进程、进程池、协程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python線程,切記Python的GIL特性

import threadingdef func():print(threading.current_thread().getName())passclass MyThread(threading.Thread):def run(self):print(threading.current_thread().getName())def main():print(threading.current_thread().getName())#創(chuàng)建線程thread = threading.Thread(target=func)thread.start()#thread.join()#創(chuàng)建線程t2 = MyThread()t2.start()t2.join()print("programs is finished.")if __name__ == "__main__":main() ?輸出

MainThread
Thread-1
Thread-2
programs is finished.

Python進程

import multiprocessing import osdef worker():print( "Child ProcessID:" + str(os.getpid()) )print("working...")def main():print( "Main ProcessID:" + str(os.getpid()) )#創(chuàng)建進程p = multiprocessing.Process(target=worker)p.start();p.join();print("programs is finished.")if __name__ == "__main__":main() ?
輸出?

Main ProcessID:33452
Child ProcessID:616
working...
programs is finished.


Python進程池

import multiprocessing from multiprocessing import Pool import os import timedef worker(i):print( "Child ProcessID:" + str(os.getpid()) )print("working..." + str(i))time.sleep(1)def callback(arg):print(arg)def main():print( "Main ProcessID:" + str(os.getpid()) )pool = Pool(4)#創(chuàng)建進程for i in range(10):pool.apply(func=worker, args=(i,))#pool.apply(func=worker, args=(i,), callback=callback)print("programs is finished.")if __name__ == "__main__":main()
輸出

Main ProcessID:27824
Child ProcessID:23104
working...0
Child ProcessID:32968
working...1
Child ProcessID:26228
working...2
Child ProcessID:31036
working...3
Child ProcessID:23104
working...4
Child ProcessID:32968
working...5
Child ProcessID:26228
working...6
Child ProcessID:31036
working...7
Child ProcessID:23104
working...8
Child ProcessID:32968
working...9
programs is finished.


Python協(xié)程一

import asyncio@asyncio.coroutine def hello():print("hello the world")r = yield from asyncio.sleep(1)print("hello again")def main():loop = asyncio.get_event_loop()"""tasks = [asyncio.ensure_future(hello()),]loop.run_until_complete(asyncio.wait(tasks))"""print("begin")loop.run_until_complete(hello())print("end")loop.close()print("program is finished.")if __name__ == "__main__":main()
Python協(xié)程二

import asyncioasync def hello():print("hello the world")r = await asyncio.sleep(1)print("hello again")def main():loop = asyncio.get_event_loop()"""tasks = [asyncio.ensure_future(hello()),]loop.run_until_complete(asyncio.wait(tasks))"""print("begin")loop.run_until_complete(hello())print("end")loop.close()print("program is finished.")if __name__ == "__main__":main()
輸出

begin
hello the world
hello again
end
program is finished.


總結(jié)

以上是生活随笔為你收集整理的Python线程、进程、进程池、协程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。