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

歡迎訪問 生活随笔!

生活随笔

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

python

python线程问题_Python线程问题

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

我有以下內(nèi)容:

d = {...} #a dictionary with strings

l1 = [...] #a list with stuff

l2 = [...] #a list with numbers

...

for i in l1:

for key in l2:

#do some stuff

...

if d[key] == i:

print d[key]

我想使用線程(為了提高性能)做同樣的事情.我想的是:

import threading

d = {...} #a dictionary with strings

l1 = [...] #a list with stuff

l2 = [...] #a list with numbers

...

def test(i, key):

#do the same stuff

if d[key] == i:

print d[j]

for i in l1:

for key in l2:

threading.start_new_thread(test, (i,key))

我不確定這是最好的方法.我擔(dān)心的是,我根本沒有優(yōu)化.一些基本想法是:

> d應(yīng)該在共享內(nèi)存中(所有線程都可以訪問它).我假設(shè)沒有線程會訪問相同的條目.

>每個(i,key)組合應(yīng)該同時進(jìn)行測試.

如果你認(rèn)為我應(yīng)該使用另一種語言,如果你能指出它我會很高興.

幫助將是ayciated.提前致謝.

解決方法:

Python(http://docs.python.org/2/library/threading.html)中的傳統(tǒng)線程在大多數(shù)常見的運(yùn)行時受到“Global Interpreter Lock” (GIL)的限制,這可以防止多個線程同時執(zhí)行,無論您擁有多少內(nèi)核或CPU.盡管存在這種限制,但傳統(tǒng)線程在線程受I / O限制時仍然非常有價(jià)值,例如處理網(wǎng)絡(luò)連接或執(zhí)行數(shù)據(jù)庫查詢,其中大多數(shù)情況下它們都在等待外部事件而不是“計(jì)算”.

multiprocessing is a package that supports spawning processes using an

API similar to the threading module. The multiprocessing package

offers both local and remote concurrency, effectively side-stepping

the Global Interpreter Lock by using subprocesses instead of threads.

Due to this, the multiprocessing module allows the programmer to fully

leverage multiple processors on a given machine.

標(biāo)簽:python,multithreading

來源: https://codeday.me/bug/20190723/1509479.html

總結(jié)

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

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