开发者

How to run a very small amount of code asynchronously?

I'd like to run a small amount of code asynchronously in my rails application. The code logs some known information. I'd like this task to not block the rest of the re开发者_Go百科sponse from my app. The operation is way too lightweight and frequent for it to be done as a delayed job tasks.

I'm thinking to just use:

Thread.new do
  # my logging code
end

and call it a day. Is this achieving what I want it to achieve? Are there any drawbacks?


It may be overkill for your particular usage, but have you considered using some form of Message Queuing Middleware such as STOMP, AMQP, OpenWire or even Jabber?

The basic outline (pseudo-code!) would be:

s = client.create_connection(user,pass,server_ip,port)
s.message_send("Log Message Goes Here")

You would then have at least one "consumer" at the other end of the message queue which would write the log message to a file/database/chatroom/IRC Channel/whatever-you-want-really-it's-all-code... :)

It would also mean that if in future you wanted to hand-off high-intensity processing jobs (invoice generation for example) you would already have the infrastructure in place to do so.

Also if you're looking for a really easy Messaging server, I recommend RabbitMQ - it's written in Erlang (but don't let that put you off!) and is very easy to setup.

This is my first post so I can't post any more than two links, but I've published links to all the technologies mentioned above in a gist @ https://gist.github.com/1090372


Aside from handling some baseline resource contention things, this should be sufficient. The database resource comes to mind if you're logging there...a file if you're logging there. But the basic approach is fine I would think. Simpler is better...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜