简体   繁体   English

调用使用多进程/多线程的Python脚本时,PHP挂起

[英]PHP hangs when call Python script that use multiprocess/multithreading

I've write a php code to call a Python script much like this: 我已经编写了一个PHP代码来调用Python脚本,如下所示:

<?php
    system("tmn", $return_value);
    echo $return_value;
?>

Below is the Python script. 以下是Python脚本。

#!/usr/bin/env python
import os
from subprocess import Popen

devnull = open(os.devnull, 'wb')

p = [] # ip -> process
for n in range(1, 20): # start ping processes
    ip = "172.28.83.%d" % n
    p.append((ip, Popen(['ping', '-c', '1', '-w', '1', ip], stdout=devnull)))
    #NOTE: you could set stderr=subprocess.STDOUT to ignore stderr also

while p:
    for i, (ip, proc) in enumerate(p[:]):
        if proc.poll() is not None: # ping finished
            p.remove((ip, proc)) # this makes it O(n**2)
            if proc.returncode == 0:
                print('%s active' % ip)
            elif proc.returncode == 2:
                print('%s no response' % ip)
            else:
                print('%s error' % ip)
devnull.close()

But when I load the php page using my broswer, the page will loading forever, it seems that PHP is stuck at the system or exec call. 但是,当我使用浏览器加载php页面时,该页面将永远加载, 看来PHP卡在了systemexec调用中。 I tried using different Python script, but as long as the script is parallel(using either Multiproccessing or Multithreading), this problem will definitely happen. 我尝试使用其他Python脚本,但是只要脚本是并行的(使用Multiproccessing或Multithreading),肯定会发生此问题。

The weirdest thing is that this issue only happens on one of my linux server(CentOS 6.5). 最奇怪的是,此问题仅发生在我的Linux服务器之一(CentOS 6.5)上。

$php -v
PHP 5.5.7 (cli) (built: Jan  3 2014 11:19:10) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies

python --version
Python 2.7.6

I've squeeze my head all day for this. 我整天为此压着头。 It would be a huge help if you give any suggestion. 如果您提出任何建议,那将是巨大的帮助。

This is probably widely off mark, but the rule of thumb solution for solving "weird problems which only happen on centos" is "have you tried disabling selinux?" 这可能是很不合理的,但是解决“仅在centos上发生的怪异问题”的经验法则是“您是否尝试禁用selinux?”

Maybe you should try disabling it ( http://www.cyberciti.biz/faq/howto-turn-off-selinux/ ), rebooting and trying your code again. 也许您应该尝试禁用它( http://www.cyberciti.biz/faq/howto-turn-off-selinux/ ),然后重新启动并重试您的代码。 If it works, you will either learn to keep selinux disabled on all your systems or you will have an excellent adventure in trying to understand how selinux works, in which case, good luck and bring a lot of aspirin. 如果它可以工作,您将学会在所有系统上禁用selinux,或者您将经历一次极好的冒险,试图了解selinux的工作原理,在这种情况下,祝您好运并带来大量的阿司匹林。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM