I encountered an issue running a Python script from a Gradle task.
Why does a Gradle task that executes a Python script, where the Python scripts spawns a process, seems to block?
I created three Python scripts.
print("Done")
(This script was used for debugging purposes.)Running all three Python scripts locally from my terminal/console performs the expected behavior.
I have a Gradle task that for each of the Python script, where the task calls the Python script. My build.gradle looks like this:
task start (type:Exec) {
workingDir './mysie'
commandLine 'python', 'start.py'
}
task stop(type:Exec) {
workingDir './mydir'
commandLine 'python', 'stop.py'
}
task testPython(type:Exec) {
workingDir './mydir'
commandLine 'python', 'test.py'
}
I'm using the Gradle wrapper executable, and if I execute gradlew testPython
, the task runs and I get a BUILD SUCCESSFUL
message.
$ /gradlew test
Parallel execution is an incubating feature.
:testPython
Done
BUILD SUCCESSFUL
Total time: 0.888 secs
So this indicates that gradlew
can successfully execute a Python script without blocking. However, when I execute gradlew start
, which spawns two processes. It's important to not that the Python script spawns the two processes, but does not terminate them. The Gradle task never completes. It appears to be blocked. Here is the output I see:
$ gradlew start
Parallel execution is an incubating feature.
:start
<Insert Python "print" statement that have been flushed here>
> Building 0% > :start
I can see the processes started on my host. Also, before start.py
exists, I print("Exiting...")
, which is displayed on the console. So I know the Python script executed and completed.
In another terminal, I execute gradle stop
, which successfully terminates the processes.
Here's the weird part. After the gradle stop
command completes successfully, the gradle start
, which was previously blocked, suddenly completes.
I need to have the start
and stop
in separate tasks. So my questions are:
gradle start
task after the Python script spawns the process?The content of build.gradle file looks correct. All the tasks are defined correctly.
Because if task is of type Exec
it waits till process is finished, then task finishes also. If two separate processes are started from python script being run from Exec
task and they're bounded somehow to script which started them (which is bounded to gradle task itself) it all must finish to allow gradle task to finish as well. Have a look at this question for instance.
By writing a custom task which will start the processes in background. See the question linked above.
Hope this helps. If there's anything unclear, just ask.