Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
synchronization in rails
5 Posts
synchronization in rails

Im new to rails. I just want to know that how to do code synchronization in rails. For an example when there are 100 or more clients are connected to my site i want to limit(synchronize) the access to a certain resource(file,etc) only for 1 client. its just like thread synchronization in c++. Cheers AINA

Ok, so firstly - just in case it's in there - get it out of your mind that you're doing anything like thread synchronization here. Like most good *nix citizens, Rails is run in separate processes, not threads. So, like all such things you have to do your own synchronization depending on the resource.

For mutexing a file access, people generally use File#flock which uses the POSIX flock(2) syscall to lock a file.

OOps.... "Rails is run in separate processes, not threads.". Does that mean Rails can only process one request at a time??? It is not possible to process two requests at the same time like using threads ?? So then how does it handle sites with huge traffic with tons of requests... Do they all have to wait on a cue to get the response???. Pls help i'm totally confused here.

Yes, each Rails process can only process one request at a time, yes. Your app server will run multiple Rails processes to handle concurrent requests. This is very common in *nix (in fact most daemons work this way, very few use threads) - nothing to be concerned about.

Out of curiosity, what is it you'd like to synchronize, and why? The discussion of threads is somewhat interesting, but may be a red herring. You still need to synchronize access to shared resources across multiple processes.

I'd recommend you use the transactional nature of your database whenever possible. Doing so also gets around the issue that you may have multiple application servers running your Rails app; which means you'd have to do something complicated and horrible to give them all access to the same file system.

5 Posts
Login to add your message