進程和線程python
❶ python3進程與線程區別
什麼是進程(Process):普通的解釋就是,進程是程序的一次執行,而什麼是線程內(Thread),線程可以理解為容進程中的執行的一段程序片段。在一個多任務環境中下面的概念可以幫助我們理解兩者間的差別:進程間是獨立的,這表現在內存空間,上下文環境;線程運行在進程空間內。 一般來講(不使用特殊技術)進程是無法突破進程邊界存取其他進程內的存儲空間;而線程由於處於進程空間內,所以同一進程所產生的線程共享同一內存空間。同一進程中的兩段代碼不能夠同時執行,除非引入線程。線程是屬於進程的,當進程退出時該進程所產生的線程都會被強制退出並清除。線程佔用的資源要少於進程所佔用的資源。進程和線程都可以有優先順序。在線程系統中進程也是一個線程。可以將進程理解為一個程序的第一個線程。
線程是指進程內的一個執行單元,也是進程內的可調度實體.與進程的區別:(1)地址空間:進程內的一個執行單元;進程至少有一個線程;它們共享進程的地址空間;而進程有自己獨立的地址空間;(2)進程是資源分配和擁有的單位,同一個進程內的線程共享進程的資源(3)線程是處理器調度的基本單位,但進程不是.(4)二者均可並發執行.
❷ Python中進程和線程的區別詳解
什麼是進程(Process):普通的解釋就是,進程是程序的一次執行,而什麼是線程(Thread),線程可以理解為進程中的執行的一段程序片段。在一個多任務環境中下面的概念可以幫助我們理解兩者間的差別:進程間是獨立的,這表現在內存空間,上下文環境;線程運行在進程空間內。 一般來講(不使用特殊技術)進程是無法突破進程邊界存取其他進程內的存儲空間;而線程由於處於進程空間內,所以同一進程所產生的線程共享同一內存空間。同一進程中的兩段代碼不能夠同時執行,除非引入線程。線程是屬於進程的,當進程退出時該進程所產生的線程都會被強制退出並清除。線程佔用的資源要少於進程所佔用的資源。進程和線程都可以有優先順序。在線程系統中進程也是一個線程。可以將進程理解為一個程序的第一個線程。
線程是指進程內的一個執行單元,也是進程內的可調度實體.與進程的區別:(1)地址空間:進程內的一個執行單元;進程至少有一個線程;它們共享進程的地址空間;而進程有自己獨立的地址空間;(2)進程是資源分配和擁有的單位,同一個進程內的線程共享進程的資源(3)線程是處理器調度的基本單位,但進程不是.(4)二者均可並發執行.
❸ 多線程 python和多進程的區別
多進程共享資源必然會帶來進程間相互競爭。
而這種競爭又會造成race condition,我們的結果有可能被競爭的不確定性所影響。
但如果需要,我們依然可以通過共享內存和Manager對象這么做。
❹ python中多進程和多線程的區別
多線程可以共享全局變數,多進程不能。
多線程中,所有子線程的進程號相同;多進程中,不同的子進程進程號不同。
多進程比線程更易用,但是消耗更多的內存。
❺ 線程和進程的區別及Python代碼實例
python的腳本讓一個進程中運行兩個線程:
import time
import thread
def Ordering(interval):
cnt = 0
while cnt<100:
print '好了,你訂餐成功,訂餐號碼是:%d號 訂餐時間是:%s 請在旁邊耐心等待\n\n'%(cnt, time.ctime())
time.sleep(interval)
cnt+=1
thread.exit_thread()
def Notice(interval):
cnt = 0
while cnt<100:
print '誰的號碼是%d,您的餐好了,過來取一下\n'%(cnt)
time.sleep(interval)
cnt+=1
thread.exit_thread()
def work(): #Use thread.start_new_thread() to create 2 new threads
thread.start_new_thread(Ordering,(1,))
thread.start_new_thread(Notice,(5,))
if __name__=='__main__':
work()
❻ python 多進程和多線程配合
由於python的多線程中存在PIL鎖,因此python的多線程不能利用多核,那麼,由於現在的計算機是多核的,就不能充分利用計算機的多核資源。但是python中的多進程是可以跑在不同的cpu上的。因此,嘗試了多進程+多線程的方式,來做一個任務。比如:從中科大的鏡像源中下載多個rpm包。
#!/usr/bin/pythonimport reimport commandsimport timeimport multiprocessingimport threadingdef download_image(url):
print '*****the %s rpm begin to download *******' % url
commands.getoutput('wget %s' % url)def get_rpm_url_list(url):
commands.getoutput('wget %s' % url)
rpm_info_str = open('index.html').read()
regu_mate = '(?<=<a href=")(.*?)(?=">)'
rpm_list = re.findall(regu_mate, rpm_info_str)
rpm_url_list = [url + rpm_name for rpm_name in rpm_list] print 'the count of rpm list is: ', len(rpm_url_list) return rpm_url_
def multi_thread(rpm_url_list):
threads = [] # url = 'https://mirrors.ustc.e.cn/centos/7/os/x86_64/Packages/'
# rpm_url_list = get_rpm_url_list(url)
for index in range(len(rpm_url_list)): print 'rpm_url is:', rpm_url_list[index]
one_thread = threading.Thread(target=download_image, args=(rpm_url_list[index],))
threads.append(one_thread)
thread_num = 5 # set threading pool, you have put 4 threads in it
while 1:
count = min(thread_num, len(threads)) print '**********count*********', count ###25,25,...6707%25
res = [] for index in range(count):
x = threads.pop()
res.append(x) for thread_index in res:
thread_index.start() for j in res:
j.join() if not threads:
def multi_process(rpm_url_list):
# process num at the same time is 4
process = []
rpm_url_group_0 = []
rpm_url_group_1 = []
rpm_url_group_2 = []
rpm_url_group_3 = [] for index in range(len(rpm_url_list)): if index % 4 == 0:
rpm_url_group_0.append(rpm_url_list[index]) elif index % 4 == 1:
rpm_url_group_1.append(rpm_url_list[index]) elif index % 4 == 2:
rpm_url_group_2.append(rpm_url_list[index]) elif index % 4 == 3:
rpm_url_group_3.append(rpm_url_list[index])
rpm_url_groups = [rpm_url_group_0, rpm_url_group_1, rpm_url_group_2, rpm_url_group_3] for each_rpm_group in rpm_url_groups:
each_process = multiprocessing.Process(target = multi_thread, args = (each_rpm_group,))
process.append(each_process) for one_process in process:
one_process.start() for one_process in process:
one_process.join()# for each_url in rpm_url_list:# print '*****the %s rpm begin to download *******' %each_url## commands.getoutput('wget %s' %each_url)
def main():
url = 'https://mirrors.ustc.e.cn/centos/7/os/x86_64/Packages/'
url_paas = 'http://mirrors.ustc.e.cn/centos/7.3.1611/paas/x86_64/openshift-origin/'
url_paas2 ='http://mirrors.ustc.e.cn/fedora/development/26/Server/x86_64/os/Packages/u/'
start_time = time.time()
rpm_list = get_rpm_url_list(url_paas) print multi_process(rpm_list) # print multi_thread(rpm_list)
#print multi_process()
# print multi_thread(rpm_list)
# for index in range(len(rpm_list)):
# print 'rpm_url is:', rpm_list[index]
end_time = time.time() print 'the download time is:', end_time - start_timeprint main()123456789101112131415161718
代碼的功能主要是這樣的:
main()方法中調用get_rpm_url_list(base_url)方法,獲取要下載的每個rpm包的具體的url地址。其中base_url即中科大基礎的鏡像源的地址,比如:http://mirrors.ustc.e.cn/centos/7.3.1611/paas/x86_64/openshift-origin/,這個地址下有幾十個rpm包,get_rpm_url_list方法將每個rpm包的url地址拼出來並返回。
multi_process(rpm_url_list)啟動多進程方法,在該方法中,會調用多線程方法。該方法啟動4個多進程,將上面方法得到的rpm包的url地址進行分組,分成4組,然後每一個組中的rpm包再最後由不同的線程去執行。從而達到了多進程+多線程的配合使用。
代碼還有需要改進的地方,比如多進程啟動的進程個數和rpm包的url地址分組是硬編碼,這個還需要改進,畢竟,不同的機器,適合同時啟動的進程個數是不同的。
❼ python如何獲取進程和線程狀態
threading.active_count()
Return the number of Thread objects currently alive. The returned count is equal to the length of the list returned by enumerate().
active_count可以返回當前活動的線程枚舉
我一般是這么用的
def getHeatsParallel(self): threads = [] for i in range(0, self.threadCount): t = threading.Thread(target=self.SomeFunction, name=str(i)) threads.append(t) t.start() for t in threads: t.join()
❽ python多進程和多線程的區別
多線程指的是一個進程有多個線程運行,線程之間能夠共用資源,但是只能靠專一個cpu運行,效率不高。
多進程指的屬是多個進程同時運行,並佔用多個cpu,實現並行,效率高,但是進程之間不能隨意共享資源,只能通過管道和進程隊列交換數據
❾ python多線程和多進程的區別有哪些
python多線程和多進程的區別有七種:
1、多線程內可以共享全局變數,多進容程不能。
2、多線程中,所有子線程的進程號相同;多進程中,不同的子進程進程號不同。
3、線程共享內存空間;進程的內存是獨立的。
4、同一個進程的線程之間可以直接交流;兩個進程想通信,必須通過一個中間代理來實現。
5、創建新線程很簡單;創建新進程需要對其父進程進行一次克隆。
6、一個線程可以控制和操作同一進程里的其他線程;但是進程只能操作子進程。
7、兩者最大的不同在於:在多進程中,同一個變數,各自有一份拷貝存在於每個進程中,互不影響;而多線程中,所有變數都由所有線程共享。
更多Python知識,請關註:Python自學網!!
❿ python線程和進程的區別
python的腳本讓一個進程中運行兩個線程:
import time
import thread
def Ordering(interval):
cnt = 0
while cnt<100:
print '好了,你訂餐成功,訂餐號碼是:%d號 訂餐時間是:%s 請在旁邊耐心等待\n\n'%(cnt, time.ctime())
time.sleep(interval)
cnt+=1
thread.exit_thread()
def Notice(interval):
cnt = 0
while cnt<100:
print '誰的號碼是%d,您的餐好了,過來取一下\n'%(cnt)
time.sleep(interval)
cnt+=1
thread.exit_thread()
def work(): #Use thread.start_new_thread() to create 2 new threads
thread.start_new_thread(Ordering,(1,))
thread.start_new_thread(Notice,(5,))
if __name__=='__main__':
work()