"Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once." - Rob Pike
Sources:
https://vimeo.com/49718712
http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil
http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html
GIL is a locking mechanism that is meant to protect your data integrity(CRuby, CPython) It force to run only one thread at a time even on a multicore processor
In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. - Wikipedia
Resources other than memory, such as network sockets, database handles, user interaction windows, and file and device descriptors, are not typically handled by garbage collection.
A well known issue in MRI
Source: https://www.youtube.com/watch?v=etCJKDCbCj4
$ ruby pushing_nil.rb
5000
$ jruby pushing_nil.rb
4446
$ rbx pushing_nil.rb
3088
array = []
5.times.map do
Thread.new do
1000.times do
array << nil
end
end
end.each(&:join)
puts array.size
VALUE
rb_ary_push(VALUE ary, VALUE item)
{
long idx = RARRAY_LEN(ary);
ary_ensure_room_for_push(ary, 1);
RARRAY_ASET(ary, idx, item);
ARY_SET_LEN(ary, idx + 1);
return ary;
}
In web each thread spend quite a lot of time in IOs which won’t block the thread scheduler. So if you receive two quasi-concurrent requests you might not even be affected by the GIL
Yehuda Katz
It allow to fork as many process as need and control them in case of hanging or memory leaks
If DB query(I/O) blocking other requests then wrap it in a fiber, trigger an async call and pause the fiber so another request can get processed. Once the DB query comes back, it wakes up the fiber it was trigger from, which then sends the response back to the client.
PROS:
CONS:
CONS:
PROS:
PROS:
heavy: sidekiq -C config/sidekiq.yml -c 3
-q document_generation, 8
-q bulk_queue, 4
-q slow_queue, 2
lite: sidekiq -C config/sidekiq.yml -c 3
-q reports, 8
-q fast_queue, 4
-q slow_queue, 2
1. https://www.youtube.com/watch?v=eHPp-tpCAZ0
2. http://en.wikipedia.org/wiki/Global_Interpreter_Lock
3. http://tonyarcieri.com/2012-the-year-rubyists-learned-to-stop-worrying-and-love-the-threads
4. https://practicingruby.com/articles/gentle-intro-to-actor-based-concurrency
5. https://github.com/celluloid/celluloid/wiki/Gotchas
6. http://merbist.com/2011/02/22/concurrency-in-ruby-explained/
7. https://en.wikipedia.org/wiki/Actor_model
8. http://www.jstorimer.com/products/working-with-ruby-threads