python在每个独立的进程中运行一个函数,python进程,#!/usr/bin/e
文章由Byrx.net分享于2019-03-23 08:03:30
python在每个独立的进程中运行一个函数,python进程,#!/usr/bin/e
#!/usr/bin/env pythonfrom __future__ import with_statementimport os, cPickledef run_in_separate_process(func, *args, **kwds): pread, pwrite = os.pipe() pid = os.fork() if pid > 0: os.close(pwrite) with os.fdopen(pread, 'rb') as f: status, result = cPickle.load(f) os.waitpid(pid, 0) if status == 0: return result else: raise result else: os.close(pread) try: result = func(*args, **kwds) status = 0 except Exception, exc: result = exc status = 1 with os.fdopen(pwrite, 'wb') as f: try: cPickle.dump((status,result), f, cPickle.HIGHEST_PROTOCOL) except cPickle.PicklingError, exc: cPickle.dump((2,exc), f, cPickle.HIGHEST_PROTOCOL) os._exit(0)#an example of usedef treble(x): return 3 * xdef main(): #calling directly print treble(4) #calling in separate process print run_in_separate_process(treble, 4)
相关内容
- 牛顿、拉复生方法求开方,牛顿拉复生开方,[Python]代码
- winmm调整windows系统的音量大小,winmm音量,[Python]代码#
- Python脚本提取谷歌音乐搜索结果,python谷歌,[Python]代码
- 股市信息下载,,[Python]代码im
- 栈类,,[Python]代码cl
- 三维矢量的幅度,三维矢量幅度,[Python]代码fr
- 素数检测器,,[Python]代码de
- Sierpinski carpet(Sierpinski 地毯),sierpinskicarpet,[Python]代
- 一个文件里有多少字?,文件里字?,[Python]代码fr
- python端口扫描程序,python端口扫描,from socket
评论关闭