忘记密码   免费注册 查看新帖 |

ChinaUnix.net

  平台 论坛 博客 文库 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
查看: 1284 | 回复: 0

commands.getstatusoutput会不会阻塞不返回 [复制链接]

论坛徽章:
0
发表于 2017-04-03 18:24 |显示全部楼层
本帖最后由 linewer 于 2017-04-03 18:25 编辑

有个程序片段,使用thread模块产生两个线程,每个用commands.getstatusoutput执行一ssh登陆远程服务执行部分命令并返回执行的结果

现在的问题是程序放到前台没啥问题,但是放到后台执行一段时间发现就stopped住了, 看了下调用堆栈,发现工作线程在read,主线程在wait. 刚开始以为是没加锁的原因,后来看了调用堆栈,感觉是一直没有读到返回结果. 初步定位应该是command模块那个函数的问题, 文档中对这个模块的解释比较少,哪位能解答下么,多谢!

  1. #!/usr/bin/python


  2. import commands
  3. import time
  4. import threading
  5. import logging

  6. result_list = []
  7. cmds = []
  8. cmd1="ssh  test1@host1 'sleep 3 && date'"
  9. cmd2="ssh  test2@host2 'sleep 3 && date'"
  10. cmds.append(cmd1)
  11. cmds.append(cmd2)

  12. def execfunc(cmd):
  13.    
  14.     global result_list
  15.     logging.debug('starting')
  16.     (status1,output1)=commands.getstatusoutput(cmd)
  17.     result_list.append(output1)
  18.     print output1
  19.     logging.debug('ending')

  20. def testfunc(cmd):
  21.     print cmd

  22. def run():
  23.     threads = []
  24.     global cmds
  25.     for i in cmds:
  26.         t = threading.Thread(target=execfunc,args=(i,))
  27.         #t = threading.Thread(target=testfunc,args=(i,))   //换成这个没有问题
  28.         t.start()
  29.         threads.append(t)
  30.     for i in threads:
  31.         logging.debug('%s-staring',i.getName())
  32.         i.join()
  33.         logging.debug('%s-ending',i.getName())


  34. def main():
  35.     while True:
  36.         global result_list
  37.         result_list = []
  38.         run()
  39.         #do something for result_list

  40.         time.sleep(10)

  41. if __name__ == '__main__':
  42.         main()
复制代码
  1. 主线程堆栈如下:
  2. #0  0x00007f2688629930 in sem_wait () from /lib64/libpthread.so.0
  3. #1  0x00007f2688936438 in PyThread_acquire_lock () from /usr/lib64/libpython2.6.so.1.0
  4. #2  0x00007f268893a324 in ?? () from /usr/lib64/libpython2.6.so.1.0
  5. #3  0x00007f268890e9e4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  6. #4  0x00007f2688910657 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
  7. #5  0x00007f268890eaa4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  8. #6  0x00007f2688910657 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
  9. #7  0x00007f268890eaa4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  10. #8  0x00007f268890fb8f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  11. #9  0x00007f268890fb8f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  12. #10 0x00007f2688910657 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
  13. #11 0x00007f2688910732 in PyEval_EvalCode () from /usr/lib64/libpython2.6.so.1.0
  14. #12 0x00007f268892abac in ?? () from /usr/lib64/libpython2.6.so.1.0
  15. #13 0x00007f268892ac80 in PyRun_FileExFlags () from /usr/lib64/libpython2.6.so.1.0
  16. #14 0x00007f268892c16c in PyRun_SimpleFileExFlags () from /usr/lib64/libpython2.6.so.1.0
  17. #15 0x00007f26889388a2 in Py_Main () from /usr/lib64/libpython2.6.so.1.0
  18. #16 0x00007f2687c1bd5d in __libc_start_main () from /lib64/libc.so.6
  19. #17 0x0000000000400649 in _start ()

  20. 工作线程堆栈如下:
  21. 0  0x00007fb082ad54dd in read () from /lib64/libc.so.6
  22. #1  0x00007fb082a6bce8 in _IO_new_file_underflow () from /lib64/libc.so.6
  23. #2  0x00007fb082a6ec0d in _IO_default_xsgetn_internal () from /lib64/libc.so.6
  24. #3  0x00007fb082a61462 in fread () from /lib64/libc.so.6
  25. #4  0x00007fb083699d2c in ?? () from /usr/lib64/libpython2.6.so.1.0
  26. #5  0x00007fb08370b9e4 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  27. #6  0x00007fb08370cb8f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  28. #7  0x00007fb08370d657 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
  29. #8  0x00007fb0836a0dad in ?? () from /usr/lib64/libpython2.6.so.1.0
  30. #9  0x00007fb083679c63 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
  31. #10 0x00007fb08370a470 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  32. #11 0x00007fb08370cb8f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  33. #12 0x00007fb08370cb8f in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0
  34. #13 0x00007fb08370d657 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0
  35. #14 0x00007fb0836a0cb0 in ?? () from /usr/lib64/libpython2.6.so.1.0
  36. #15 0x00007fb083679c63 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
  37. #16 0x00007fb08368c6af in ?? () from /usr/lib64/libpython2.6.so.1.0
  38. #17 0x00007fb083679c63 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0
  39. #18 0x00007fb083705c93 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.6.so.1.0
  40. #19 0x00007fb0837377ba in ?? () from /usr/lib64/libpython2.6.so.1.0
  41. #20 0x00007fb0834209d1 in start_thread () from /lib64/libpthread.so.0
  42. ---Type <return> to continue, or q <return> to quit---
  43. #21 0x00007fb082ae28fd in clone () from /lib64/libc.so.6
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:wangnan@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP