Python 关于Popen.wait阻碍通信
来源:互联网 发布:为什么登不上talk软件 编辑:程序博客网 时间:2024/04/29 20:01
环境:ubuntu12.04 python2.7
今天在平台的一个功能模块中出问题了。函数最后会启动一个协程,如下:
greenlet = gevent.Greenlet(self.do_sth, arg1, arg2)
greenlet.start()
然后在def do_sth(arg1, arg2)中会调用一些系统命令来执行任务,我采用的subprocess.Popen,如下:
p = Popen(cmd1, shell=True)
p.wait()
等于在启动的协程中调用了popen.wait。
问题出来了,我在执行这个功能模块的过程中,发现不能执行其他的功能了
原因应该是popen.wait阻碍了socket通信什么的,查找资料:
Popen.wait() 等待子进程结束。设置并返回returncode属性 :http://blog.csdn.net/wuwangyingzhong/article/details/6002055
这样,当cmd1执行时,与进程相关的socket通信应该就被阻碍了。
据说有避免的方法:
http://www.bitscn.com/os/linux/200604/7172.html
http://ldl.wisplus.net/2010/10/01/linux%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%EF%BC%9A%E7%AE%A1%E9%81%93/
这两个都没有仔细看,也不知道说的啥,更不用说验证了。因为时间有点急,所以就避开这个方法。
我的解决办法:
因为调用了几次popen,一下子也找不到完好的替代方法,所以另找一个办法。既然与进程相关的通信被阻碍了,那我就用一个不会影响到通信的方法,那么另外启动一个线程好了。所以我改动了调用popen的协程。
将功能模块最后的协程调用改为线程调用,如下:
t = threading.Thread(target=self.do_sth,args=(arg1, arg2))
t.start()
而def do_sth(arg1, arg2)中的一些具体的代码就不用改动了。
然后我的问题貌似解决了
最后,看到thread也可以用Join阻塞进程:http://blog.csdn.net/cain/article/details/6602921
最后,我对这些不熟悉,如果有哪位看到我的解说有错误,烦请告知!
- Python 关于Popen.wait阻碍通信
- 关于python subprocess的popen
- Python Popen communicate() 和wait()使用上的区别
- Python Popen communicate()和wait()使用上的区别
- Python 线程通信(wait(), set())
- python的os模块fork、wait、system、exec、popen、exit函数讲解
- 关于线程同步通信 wait() notify() notifyAll()
- 关于python中os.system,os.popen,os.comands
- python subprocess Popen
- python subprocess Popen (转)
- python subprocess Popen
- python subprocess Popen (转)
- python Popen使用
- python 关闭 os.popen()
- 避免python Popen阻塞
- python+subprocess.popen
- Python和其他进程的管道通信方式--popen和popen2的比较
- python os.popen 超时问题
- centos打开Android Studio后出现的错误:adb: error while loading shared libraries: libncurses.so.
- opengl学习及问题总结
- eXtremeComponents 使用总结
- 感悟
- FLV文件格式详解
- Python 关于Popen.wait阻碍通信
- 使用SVD求最小二乘刚性转置
- experiment : test wdk's cdo sample in minifilter
- 转Complete Checklist for Manual Upgrades to 11gR2 [ID 837570.1]
- linux信号实现浅析2--信号发送内核源码解析
- C/C++ Linux 程序员必须了解的 10 个工具
- 大一历程回顾
- traceDebug
- 九度OJ 1008最短路径问题