简体   繁体   中英

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.

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.

Anybody have an idea why this happen?

If i try the python script i my console everything is picture perfect.!

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).

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.

Just add set_time_limit(120) at the beginning of your php code

See 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 . It will be faster this way. See php shell_exec() vs exec()

The script is now working as i should do :) Thanks to "neubert" for the answer.

If i set the $ssh->setTimeout() to unlimited, whould i still get a respons if the script halts at some point?

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