0

When I click button 1 it starts the Python file. When I click button 2 it waits for button 1's AJAX call to complete its execution. I don't want that.

Button 2 creates a text file to kill that infinite Python program (which checks for the existence of that text file at regular intervals).

In short, I am not able to create a text file (button 2's AJAX) when my Python file is running (button1's AJAX)

$(document).ready(function() {
  $("#s1").on("click", function(e) {
    console.log("hi s1");
    e.preventDefault();

    $.ajax({
      async: true,
      url: "file1.php",
      type: "GET",
      data: {
        action: "start"
      },
      success: function(response) {
        console.log("The ajax request succeeded!");
      }
    });
  });

  $("#s2").on("click", function(e) {
    console.log("hi s2");
    e.preventDefault();

    $.ajax({
      async: true,
      url: "file1.php",
      type: "GET",
      data: {
        action: "start2"
      },
      success: function(response) {
        console.log("The ajax request succeeded!");
      }
    });
  });
});
// php code for button1 
if (isset($_GET['action']) && strcasecmp($_GET['action'], "start1") == 0)
{
  $command = escapeshellcmd("python demo.py");
  // python file is a infite loop always running program
  $output = shell_exec($command);
  // echo $output;
}

// php code for button2
if (isset($_GET['action']) && strcasecmp($_GET['action'], "start2") == 0)
{
  echo "Activity stopped";
  $myfile = fopen("demo.txt", "w");
  $txt = "Example\n";
  fwrite($myfile, $txt);
  fclose($myfile);
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    If these are happening in series, not parallel, it'll be because of the server side of it (the PHP, web server, config, etc.). The client-side (JavaScript) is not making the second call wait for the first to complete. – T.J. Crowder Apr 08 '21 at 13:12
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Apr 08 '21 at 13:13
  • 1
    Also, running an infinitely looping Python script called from PHP via JS AJAX in order to determine when a file has been created seems like a needlessly convoluted solution. I'm not sure exactly what you're trying to achieve here, but I am 100% certain there's a better approach which will avoid your current async/concurrency issues. – Rory McCrossan Apr 08 '21 at 13:15
  • and what's in demo.py? what's the $output – kubarik Apr 08 '21 at 13:48
  • @T.J.Crowder Yeah, I also think so. Thanks bro. Actually my second call to php just creates a .txt file. Is there any way that I can create a .txt file from client side JS. So i wont need any of jquery, ajax, php calls. Thanks – Meet Sheth Apr 09 '21 at 05:07
  • @RiggsFolly Yes bro...Actually I do have a good code indentation in my original code. It's just that I was in a hurry and had to edit some parts of code which was not relevant to the issue. So in the question identation was disoriented here and there. – Meet Sheth Apr 09 '21 at 05:09
  • @RoryMcCrossan No its not like that. Lemme explain you the whole code. My first button starts a python program for face mask detection (Deep learning). Now its an endless program. I want a UI to start and stop my mask detector (python) . So to stop my python script , what I simply do is , create a .txt file (button 2 event). And my python programs checks for the existence of that file after every iteration. If file exists, my python program terminates ( sys.exit() ) – Meet Sheth Apr 09 '21 at 05:13
  • @kubarik demo.py is a face mask detector program. It open webcam and detects if a person has worn a mask or not. So basically its and endless program. Read the above comment to exactly know what I am trying to achieve. – Meet Sheth Apr 09 '21 at 05:15
  • Everyone please refer to this issue https://stackoverflow.com/questions/67015889/how-to-execute-shell-commands-from-php-and-do-not-wait-for-output-on-windows – Meet Sheth Apr 09 '21 at 06:06

0 Answers0