I have Apache server running on my pi. I send a httprequest from my Java app to the server to turn the motor on and off by executing a pyhton script. PHP code on the server:

```
<?php
 if(isset($_POST["on"])){
 echo "turning on";
 exec("python /home/pi/motor/on.py /dev/null/ 2>/dev/null/ &")
}

if(isset($_POST["off"])){
 echo "turning off";
 exec("python /home/pi/motor/off.py")
}
?>

```

I think my problem is when turning the motor on, my application freezes as the exec call has not returned yet (because my python code is in a loop and can only exit when interrupt arrives). I want to be able to turn the motor on and off, but I can only turn it on.
Any solutions for this problem or another way of achieving what I want to do?