Là một dự án mã nguồn mở, grosser/parallel đã đạt 4.3k sao trên GitHub, ngôn ngữ Ruby. Ruby: parallel processing made simple and fast
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Run any code in parallel Processes(> use all CPUs), Threads(> speedup blocking operations), or Ractors(> use all CPUs).
Best suited for map-reduce or e.g. parallel downloads/uploads.
Install
gem install parallel
Usage
# 2 CPUs -> work in 2 processes (a,b + c)
results = Parallel.map(['a','b','c']) do |one_letter|
SomeClass.expensive_calculation(one_letter)
end
# 3 Processes -> finished after 1 run
results = Parallel.map(['a','b','c'], in_processes: 3) { |one_letter| SomeClass.expensive_calculation(one_letter) }
# 3 Threads -> finished after 1 run
results = Parallel.map(['a','b','c'], in_threads: 3) { |one_letter| SomeClass.expensive_calculation(one_letter) }
# 3 Ractors -> finished after 1 run
results = Parallel.map(['a','b','c'], in_ractors: 3, ractor: [SomeClass, :expensive_calculation])
Same can be done with each
Parallel.each(['a','b','c']) { |one_letter| ... }
or each_with_index, map_with_index, flat_map
Produce one item at a time with lambda (anything that responds to .call) or Queue.
Parallel.any?([1,2,3,4,5,6,7]) { |number| number == 4 }
# => true
Parallel.all?([1,2,nil,4,5]) { |number| number != nil }
# => false
Processes/Threads are workers, they grab the next piece of work when they finish.
Processes
Speedup through multiple CPUs
Speedup for blocking operations
Variables are protected from change
Extra memory used
Child processes are killed when your main process is killed through Ctrl+c or kill -2
Threads
Speedup for blocking operations
Variables can be shared/modified
No extra memory used
Ractors
Ruby 3.0+ only
Speedup for blocking operations
No extra memory used
Very fast to spawn
Experimental and unstable
start and finish hooks are called on main thread
Variables must be passed in Parallel.map([1,2,3].map { |i| [i, ARGV, local_var] }, ...
use Ractor.make_shareable to pass in global objects
ActiveRecord
Connection Lost
Multithreading needs connection pooling, forks need reconnects
Adjust connection pool size in config/database.yml when multithreading
# reproducibly fixes things (spec/cases/map_with_ar.rb)
Parallel.each(User.all, in_processes: 8) do |user|
user.update_attribute(:some_attribute, some_value)
end
User.connection.reconnect!
# maybe helps: explicitly use connection pool
Parallel.each(User.all, in_threads: 8) do |user|
ActiveRecord::Base.connection_pool.with_connection do
user.update_attribute(:some_attribute, some_value)
end
end
# maybe helps: reconnect once inside every fork
Parallel.each(User.all, in_processes: 8) do |user|
@reconnected ||= User.connection.reconnect! || true
user.update_attribute(:some_attribute, some_value)
end
NameError: uninitialized constant
A race happens when ActiveRecord models are autoloaded inside parallel threads
in environments that lazy-load, like development, test, or migrations.
To fix, autoload classes before the parallel block with either require '<modelname>' or ModelName.class.
Break
Parallel.map([1, 2, 3]) do |i|
raise Parallel::Break # -> stops after all current items are finished
end
Parallel.map([1, 2, 3]) { |i| raise Parallel::Break, i if i == 2 } == 2
Kill
Only use if whatever is executing in the sub-command is safe to kill at any point
Parallel.map([1,2,3]) do |x|
raise Parallel::Kill if x == 1# -> stop all sub-processes, killing them instantly
sleep 100 # Do stuff
end
Processes vs Wire serializer: security risk for hardened environments
Worker processes talk to the parent over an anonymous pipe using Marshal by default.
If you've hardened your host against ptrace//proc/<pid>/mem access (e.g. ptrace_scope >= 2) and
want to also close the /proc/<pid>/fd/<n> pipe-reopen vector, use the HMAC serializer.
It length-prefixes and HMAC-SHA256 signs each message with a per-worker secret generated before fork,
so a same-UID attacker that reopens the pipe can't inject a forged Marshal payload into the parent (which would be RCE).
Raises SecurityError on mismatch (not a StandardError).
[Benchmark/Test] Disable threading/forking with in_threads: 0 or in_processes: 0, to run the same code with different setups
[Isolation] Do not reuse previous worker processes: isolation: true
[Fast worker shutdown] Workers skip at_exit hooks and VM teardown, which are slow in large processes: exit!: true
[Stop all processes with an alternate interrupt signal] 'INT' (from ctrl+c) is caught by default. Catch 'TERM' (from kill) with interrupt_signal: 'TERM'
[Process count via ENV] PARALLEL_PROCESSOR_COUNT=16 will use 16 instead of the number of processors detected. This is used to reconfigure a tool using parallel without inserting custom logic.
[Process count] parallel uses a number of processors seen by the OS for process count by default. If you want to use a value considering CPU quota, please add concurrent-ruby to your Gemfile.
TODO
Replace Signal trapping with simple rescue Interrupt handler
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/grosser/parallel là nguồn chính thức.
grosser/parallel có bao nhiêu sao?
grosser/parallel có 4.3k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/grosser/parallel. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
grosser/parallel có phải mã nguồn mở không?
Có — grosser/parallel phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/grosser/parallel.
grosser/parallel còn đang phát triển không?
Commit gần nhất trên grosser/parallel là 1 tháng trước (theo timestamp GitHub). Repo có 269 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
grosser/parallel dùng license gì?
grosser/parallel phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
grosser/parallel là gì?
grosser/parallel (grosser/parallel) là dự án Ruby trên GitHub. Theo mô tả gốc: Ruby: parallel processing made simple and fast
grosser/parallel viết bằng ngôn ngữ gì?
grosser/parallel chủ yếu viết bằng Ruby. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc parallel có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về parallel.