0

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

user3502250
  • 343
  • 1
  • 5
  • 18
  • It doesn't return result from what? – martin Jul 07 '16 at 14:04
  • From the Item array. If remove the first time.sleep the script works when i call it from php, and returns everything. So strange. – user3502250 Jul 07 '16 at 14:05
  • You mean the `for item in items:` loop? – martin Jul 07 '16 at 14:08
  • Sory just updated, it seems i can not use Time.sleep at all when calling the script from Php, what can be the problem? – user3502250 Jul 07 '16 at 14:11
  • Yes Martin i need 2 keep the 2 time sleep functions, but iam not sure why and how my script breaks when i call from php. – user3502250 Jul 07 '16 at 14:17
  • How it breaks? It's most likely not related to `time.sleep()`. – martin Jul 07 '16 at 14:19
  • Well if the problem isn't the time.sleep() then i think its very strange that the script works, when i remove time.sleep(). Don't you think so Martin? – user3502250 Jul 07 '16 at 14:22
  • One thing to keep in mind with phpseclib is that phpseclib times out after 10 seconds. If you're doing 3x 5 second delay that's 15 seconds, at minimum, that it'd take the script to run, which is > 10s, whereas 3x 3 is < 10s. So maybe try increasing phpseclib's timeout. You can do this by doing `$ssh->setTimeout(60)` right before you do the `$ssh->exec()` call. – neubert Jul 07 '16 at 14:46
  • Thanks Neubert :) That worked is there away i can get a respond back from the script if i took longer then lets say 60 seconds to run? :) Thanks alot. – user3502250 Jul 07 '16 at 14:51
  • @user3502250 - you can do `$ssh->setTimeout(0)` but that should be done before the exec() call and after you've logged in. If you do it before you login phpseclib will use what you passed in as the timeout parameter for `fsockopen` and `fsockopen` will fail with a 0 timeout. – neubert Jul 07 '16 at 15:30

2 Answers2

0

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

Community
  • 1
  • 1
user2740652
  • 361
  • 3
  • 12
  • In the end i think the script is going to run for maybe an hour or two, then it whould be best to put it in a queue, im using laravel so i think to add it to the queue there. Whould that work? – user3502250 Jul 07 '16 at 15:05
  • If your server is a linux distribution you can use cron to execute a php or python script every hour, day .... It's really not recommended to do php script that take more than a minute to execute. You should use something else. What you could do is use mysql,sqlite or any other database system to store the result and the web php script will get the latest result. There are lot of python module for database – user2740652 Jul 08 '16 at 06:14
0

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?

user3502250
  • 343
  • 1
  • 5
  • 18