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);
}