【问题标题】:Php system() hangs?PHP系统()挂起?
【发布时间】:2026-01-31 02:00:01
【问题描述】:

我有程序在哪里:

  • 在 php 中运行无限循环,获取新记录进行处理,保存在文件中
  • 在池脚本处理此记录的位置运行 python 脚本

php代码:

while(true) {
    $records = get_records();
    file_put_contents($file_name, json_encode($records));
    echo "php:system:before";
    system("python script.py $file_name");
    echo "php:system:before";
}

python 代码:

import json
import os
import sys
import time
import random
import urllib2
from multiprocessing import Pool, freeze_support
from subprocess import Popen, PIPE
from os.path import isdir
from platform import system
from lib import Logger

def do_something(record):
    # ... some operations
    print("python:record:done")

if __name__ == '__main__':
    records = read_records
    pool = Pool(4)
    pool.map(do_something, records)
    pool.close()
    pool.join()
    print("python:done")

但有时进程会挂起。

我的日志:

php:system:start
python:record:done
python:record:done
python:record:done
python:record:done
python:record:done
python:record:done
python:record:done
python:done
<- here I expect php:system:before

但是一天两次我没有得到它,我重置了程序。 错误在哪里,为什么脚本会挂起?

【问题讨论】:

    标签: php python system


    【解决方案1】:

    这是 Apache 中的一个已知错误:

    https://bugs.php.net/bug.php?id=44942

    【讨论】:

    • 你确定这是在 apache 后面运行的吗?看起来更像是一个 shell 脚本/cronjob。
    • 啊,好吧。你为什么使用 php 来处理这一点,而 python 来处理其余部分?为什么不直接使用 python
    • 嗨。这不是我的决定。系统要求是用 php 编写的系统,但接下来我需要 Python 的多处理模块,它是系统 hack。是否有其他选项可以从 php 运行外部程序?
    • 您需要电话的回复吗?
    • 我不需要电话回复。