在python中,任务并发一种方式是通过进程分支来实现的.在linux系统在,通过fork()方法来实现进程分支.
1.fork()调用后会创建一个新的子进程,这个子进程是原父进程的副本.子进程可以独立父进程外运行.
2.fork()是一个很特殊的方法,一次调用,两次返回.
3.fork()它会返回2个值,一个值为0,表示在子进程返回;另外一个值为非0,表示在父进程中返回子进程ID.
以下只能在linux中运行,不能在window下运行.
进程分支fork()
实例如下:
#!/usr/bin/python
#coding=utf-8
import os
def child():
print('hello from child', os.getpid())
os._exit(0)
def parent():
pid = os.fork()
if pid == 0:
child()
print 'fork child process error!'#如果打印该字符串,说明调用child()出错
else:
print('hello from parent', os.getpid(), pid)
parent()
运行结果如下:
('hello from parent', 29888, 29889)
('hello from child', 29889)
从结果不难看出, child()后的print字符并没有打印处理,说明调用child()是没有返回的.
fork和exec的组合
从上面的例子来看,调用child()方法后就直接退出了.但在实际的应用中,我们希望分支出来的子进程能独立运行另外一个新的程序.这时需要用到exec方法替换子进程,并且替换后进程的pid不会改变.exec方法不会返回.
首先解释一下exec相关的8个方法组:
os.execv(program, cmdargs)
基本的”v”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行参数字符的列表或元组.
os.execl(program, cmdarg1, cmdarg2, …, cmdargN)
基本的”l”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行多个字符参数.
os.execvp(program, args)
“p”模式下,基本的”v”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行参数字符的列表或元组.运行新程序的搜索路径为当前文件的搜索路径.
os.execlp(program, cmdarg1, cmdarg2, …, cmdargN)
“p”模式下,基本的”l”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行多个字符参数.运行新程序的搜索路径为当前文件的搜索路径.
os.execve(program, args, env)
“e”模式下,基本的”v”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行参数字符的列表或元组.最后还要传入运行新程序的需要的环境变量env字典参数.
os.execle(program, cmdarg1, cmdarg2, …, cmdargN, env)
“e”模式下,基本的”l”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行多个字符参数.最后还要传入运行新程序的需要的环境变量env字典参数.
os.execvpe(program, args, env)
在”p”和”e”的组合模式下,基本的”v”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行参数字符的列表或元组.最后还要传入运行新程序的需要的环境变量env字典参数.运行新程序的搜索路径为当前文件的搜索路径.
os.execlpe(program, cmdarg1, cmdarg2, …, cmdargN, env)
在”p”和”e”的组合模式下,基本的”l”执行形式,需要传入可执行的程序名,以及用来运行程序的命令行多个字符参数.最后还要传入运行新程序的需要的环境变量env字典参数.运行新程序的搜索路径为当前文件的搜索路径.
newprocess.py代码如下:
#!/usr/bin/python
#coding=utf-8
import os
def child():
print('hello from child', os.getpid())
os._exit(0)
child()
主代码如下:
#!/usr/bin/python
#coding=utf-8
import os
def child():
print('hello from child', os.getpid())
os._exit(0)
def parent():
pid = os.fork()
if pid == 0:
os.execlp('python', 'python', 'newprocess.py')
assert False, 'fork child process error!'
else:
print('hello from parent', os.getpid(), pid)
parent()
输出如下:
$ python TestFork.py
('hello from parent', 30791, 30792)
$ ('hello from child', 30792)

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具