0

I have created a site in laravel.

Where i use below code to get result from an external python script.

$ssh = new \phpseclib\Net\SSH2("11.111.11.11", 22);
ignore_user_abort(true);
set_time_limit(0);
$ssh->setTimeout(0);
$result = $ssh->exec("python /url_to_remote_python_file $site $keywords ", $output, $ret_code);

If i get an array back that takes arround 3 minutes to complete everything works fine.

I just found out that the problem is that the php call is hanging for a very long time, when the python script has finished its not sending anything back so the php call just hangs, how can that be?

But when i request a larger array from my python script i get a timeout with below code.

502 bad gateway nginx 1.8.0

I tried to follow the link Prevent nginx 504 Gateway timeout using PHP set_time_limit() but i am still getting 502 bad gateway when calling exsternal python script, can the exsternal server be the problem?

I whould like to make it work manualy before making it run as a cron job, whould that at all be possible.?

I have tried to put the script in laravel queue system. But there it only works if the script runs for about 3 minutes. If the script runs for, lets say 10 minutes the queue listener and log doesn't return anything back to me.

How can i call a python script from php and make sure i get a result back.

I need to run my script every night at around twelve a clock to populate my database when new results, but i need to make it work manualy first.

What is the best way to make this happen?

Community
  • 1
  • 1
user3502250
  • 343
  • 1
  • 5
  • 18
  • 1
    Possible duplicate of [Prevent nginx 504 Gateway timeout using PHP set\_time\_limit()](http://stackoverflow.com/questions/16002268/prevent-nginx-504-gateway-timeout-using-php-set-time-limit) – KikiTheOne Jul 12 '16 at 11:51
  • I will take a look at the link @KikiTheOne do u have any good idea to how i can make the script run each night at arround 12 a clock? – user3502250 Jul 12 '16 at 11:56
  • as @Bruno already said. u can use a cronjob to do so. if u dont want to use a cronjob u can use Ajax->php if u adjust the timeout. and while running u set the database with a flag so no user can write stuff or Change stuff till the Ajax->php is rdy. this way u can do this stuff the first time a user calls ur site after 12 o clock. – KikiTheOne Jul 12 '16 at 12:04
  • Thanks @KikiTheOne im gonna look into php-cli and see if i should set it up as a cronjob, if i whould like to manualy click a button and start the script call, then i need to change my php.ini settings timeout correct? Is there no timeout when i do it from crom? – user3502250 Jul 12 '16 at 12:19
  • there still is a timeout. cause ur Server gets no Response from ur Server. :) – KikiTheOne Jul 12 '16 at 13:43
  • @KikiTheOne Allright so the only way is to do it by Cron, as i understand it? So i cant make a call from php to exsternal server if i takes more then 20 minutes or so. is that correct? What is that u mean when to write ajax, should i just make an ajax call instead? wouldn't i get a timeout that way to? – user3502250 Jul 12 '16 at 13:53
  • typically its 0 http://stackoverflow.com/questions/4148830/what-is-jquerys-ajax-default-timeout-value so u dont have to worry about an ajax timeout. the most clean way is to use a cron . this way u dont have to call anything. ur cron does.BUT u still have to set the timeout up e.g. 30 mins. – KikiTheOne Jul 12 '16 at 17:50
  • Hi @KikiTheOne thanks for the answer i just found out, that the call i make to the exsternal puthon script, works and gets done but my php call is hanging. Do u have an idea why this happens? – user3502250 Jul 12 '16 at 18:38
  • maybe its about the "exec" . u cann try other commands. just search in SO for Shell_exec() or passthru(). atm i have no time to help so deep but i try when "work" calmed down :) – KikiTheOne Jul 13 '16 at 06:34
  • Thanks @KikiTheOne I just found out that when the python script is called it generates the result i need, but its like the return of the result to php gets lots. I also tried it in commandline and its the same. Everything over 10 items in the array from python then it dosent work, but if i only have 10 items to return back it works. So strange. – user3502250 Jul 13 '16 at 12:10
  • post the Code from the python ? – KikiTheOne Jul 14 '16 at 06:12

1 Answers1

0

Your problem is with your web server. If your script is supposed to run as a cron job, you don't need a web server, so just write it as a command-line script (using php-cli).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • In the end i would like to run it as a cron job, but right now i want to run it manually so i can ensure it works as expected. Is that at all possible? – user3502250 Jul 12 '16 at 13:09
  • If you can't run it "manually" (from the command line that is) you can't run it as cron job obviously... – bruno desthuilliers Jul 12 '16 at 14:55