How to execute commands asynchronously?
zhangbincheng1997 opened this issue · 2 comments
shell.run('python test.py')
shell.run('python test.py &')
shell.run('nohub python test.py &')
When performing a time-consuming task, how to make the process run in the background, and lua resty returns directly.
It's already async (it doesn't block), but the result is also implicitly awaited. If you don't want that, you can use nginx.timer.at
to schedule it in a timer context.
Basically, wrap your shellcode in a function, and then call ngx.timer.at(delay, your_function)
. This will return immediately, and run the function in the background after delay
seconds (set this to 0
for instant execution). I'd recommend to construct this function inline and close over all local variables you need. Be aware that not all nginx APIs might be available in the timer context. Another thing to mention is that the amount of concurrent timers is limited, so if you don't already have rate limiting on the caller, you should coordinate work with a queue (shm list or similar).
Thank you, nginx.timer.at() is useful!!!