Async operation in openresty

475 views
Skip to first unread message

Bogdan Irimia

unread,
Feb 26, 2015, 7:08:21 AM2/26/15
to openre...@googlegroups.com
Hello again

I know I might ask a question which has been asked many times before, but I can't find a definitive answer now.

I am in the situation where I need to implement a task that might take some time, and I don't want to block the user waiting for an answer.
I tried to implement this with light threads (ngx.thread.spawn), but I noticed that when I try to finish the main thread with ngx.exit(ngx.OK) the light thread also gets killed. If I don't use ngx.exit, the main thread keeps running until the light thread finishes, and this result in a timeout on the client's side.

Is it possible to launch an asynchronous task and then to immediately return a response to the HTTP request that I received?

Thank you

Bogdan

James Hurst

unread,
Feb 26, 2015, 7:24:37 AM2/26/15
to openre...@googlegroups.com
Hi,

Take a look at ngx.eof() [1]. The client timeout you're seeing might depend on the protocol version of the request or the client.

I ended up with quite an over-engineered solution for background work, because I have tasks which *must* be completed, and cannot be dropped etc. For this, I wrote a binding [2] to qless-core [3], which is a Redis based job/queue system implemented as Lua scripts within Redis. I run workers [4] for this in init_worker_by_lua, which pick off the jobs and process them reliably. So this is quite complicated, but reliable.


James.


--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Bogdan Irimia

unread,
Feb 26, 2015, 7:44:31 AM2/26/15
to openre...@googlegroups.com
Wow, excellent! Just the answer I've been looking for - very professional.

I think the ngx.eof way is just right for us right now, but the qless alternative is an excellent thing to keep in mind.

Thank you

Bogdan

Thursday, February 26, 2015 14:24
Hi,

Take a look at ngx.eof() [1]. The client timeout you're seeing might depend on the protocol version of the request or the client.

I ended up with quite an over-engineered solution for background work, because I have tasks which *must* be completed, and cannot be dropped etc. For this, I wrote a binding [2] to qless-core [3], which is a Redis based job/queue system implemented as Lua scripts within Redis. I run workers [4] for this in init_worker_by_lua, which pick off the jobs and process them reliably. So this is quite complicated, but reliable.


James.





--
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thursday, February 26, 2015 14:08
Reply all
Reply to author
Forward
0 new messages