简体   繁体   中英

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
    system("tmn", $return_value);
    echo $return_value;
?>

Below is the Python script.

#!/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. I tried using different Python script, but as long as the script is parallel(using either Multiproccessing or Multithreading), this problem will definitely happen.

The weirdest thing is that this issue only happens on one of my linux server(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?"

Maybe you should try disabling it ( http://www.cyberciti.biz/faq/howto-turn-off-selinux/ ), rebooting and trying your code again. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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