简体   繁体   English

Python到PHP奇怪的错误

[英]Python to PHP strange error

I hope somebody can help me. 我希望有人能帮助我。 :) :)

I am calling a python script from PHP, with the help from phpseclib/ssh2 i ssh into my server and it works fine. 我从PHP调用一个python脚本,从phpseclib / SSH2帮助我ssh到我的服务器,它工作正常。

My problem is that if i use "time.sleep(5)" in the loop of my python script i dont get a result back, but if i remove time.sleep(5) and time.sleep(3) it works. 我的问题是,如果我使用“time.sleep(5)”在我的python脚本的环我没有得到一个结果回来,但如果我删除time.sleep(5)和time.sleep(3)它的工作原理。

Anybody have an idea why this happen? 任何人有一个想法,为什么出现这种情况?

If i try the python script i my console everything is picture perfect.! 如果我尝试的Python脚本我我的控制台一切都是完美的图片!

items = [
    '1',
    '2',
    '3'
]
    itemArray = {}
    def checker():
        for item in items:
            time.sleep(5) # If added not working, if removed working, result gets send back
            position = 1  # keeps track of the ranking position
            for start in range(int(deep)):
                time.sleep(3)
                results = 'something'
                for div in results:
                    try:
                        if div.find('i', href=True)['href'].find(something) != -1:
                            exit_conditon = True
                            break
                        else:
                            position += 1
                    except:
                        print "Unexpected error:", sys.exc_info()[0]
                        raise
                if 'exit_conditon' in locals():
                    if exit_conditon is True:
                        exit_conditon = False
                        itemArray.update({value: 1})
                        break

        sys.exit(itemArray)
    checker() 

Please help. 请帮忙。

Update: if i have 3 rows in the items array i need to remove the second time.sleep(5) to get it working, if i have 2 items in my array i only need to remove the first time.sleep(5). 更新:如果我有项目阵列中的3行我需要删除(5)得到它的工作的第二time.sleep,如果我在我的阵列2项,我只需要删除第一time.sleep(5)。

It depends how long your script take time to execute. 这取决于您的脚本需要花费多长时间来执行。 If your script (php + python) take longer than 30 second with the default config, them php kill it. 如果您的脚本(php + python)在默认配置下花费的时间超过30秒,则它们会用php杀死它。

Just add set_time_limit(120) at the beginning of your php code 只需在您的php代码的开头添加set_time_limit(120)

See http://php.net/manual/en/function.set-time-limit.php 参见http://php.net/manual/en/function.set-time-limit.php

And If you are executing the php and python script on the same server you should use exec or shell_exec . 而且,如果要在同一服务器上执行php和python脚本,则应使用execshell_exec It will be faster this way. 这样会更快。 See php shell_exec() vs exec() 参见php shell_exec()vs exec()

The script is now working as i should do :) Thanks to "neubert" for the answer. 该脚本现在正在按我的要求进行工作:)感谢“ neubert”的回答。

If i set the $ssh->setTimeout() to unlimited, whould i still get a respons if the script halts at some point? 如果我将$ ssh-> setTimeout()设置为无限制,如果脚本在某个时间点停止,我仍会得到响应吗?

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

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