python創(chuàng)建進程fork用法
更新時間:2015年06月04日 10:51:30 作者:MaxOmnis
這篇文章主要介紹了python創(chuàng)建進程fork用法,實例分析了Python使用fork創(chuàng)建進程的使用方法,需要的朋友可以參考下
本文實例講述了python創(chuàng)建進程fork用法。分享給大家供大家參考。具體分析如下:
#!coding=utf-8
import os ,traceback
import time
'''
fork()系統(tǒng)調(diào)用是Unix下以自身進程創(chuàng)建子進程的系統(tǒng)調(diào)用,
一次調(diào)用,兩次返回,如果返回是0,
則是子進程,如果返回值>0,則是父進程(返回值是子進程的pid)
'''
source = 10
i = 0
try:
print '***********************'
pid = os.fork()
#這里會返回兩次,所以下面的省略號會輸出2次
print '......'
if pid == 0:#子進程
print "this is child process"
source = source - 1
print 'child process source is ',source
time.sleep(10)
print 'child sleep done'
else: #父進程
print "this is parent process"
print 'parent process source is ',source
time.sleep(10)
print 'parent sleep done'
print source
except:
traceback.print_exc()
輸出如下:
*********************** ...... this is child process child process source is 9 ...... this is parent process parent process source is 10 child sleep done 9 parent sleep done 10
希望本文所述對大家的Python程序設計有所幫助。
相關(guān)文章
Python實現(xiàn)截圖生成符合markdown的鏈接
之前是用的是typora來寫的文章,最近typora最近開始收費了,所以就不想用了,于是找到了一個替代品MarkText。本文將介紹如何通過Python實現(xiàn)截圖自動生成符合markdown的鏈接,感興趣的可以了解一下2022-01-01
解決python通過cx_Oracle模塊連接Oracle亂碼的問題
今天小編就為大家分享一篇解決python通過cx_Oracle模塊連接Oracle亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python處理JSON數(shù)據(jù)并導入Neo4j數(shù)據(jù)庫
在數(shù)據(jù)處理和分析中,JSON是一種常見的數(shù)據(jù)格式,Neo4j是一個高性能的圖數(shù)據(jù)庫,能夠存儲和查詢復雜的網(wǎng)絡關(guān)系,下面我們就來看看Python如何處理JSON數(shù)據(jù)并導入Neo4j數(shù)據(jù)庫吧2024-11-11
Python本地搭建靜態(tài)Web服務器的實現(xiàn)
本文主要介紹了Python本地搭建靜態(tài)Web服務器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
python opencv捕獲攝像頭并顯示內(nèi)容的實現(xiàn)
這篇文章主要介紹了python opencv捕獲攝像頭并顯示內(nèi)容的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07

