Skip to content

Instantly share code, notes, and snippets.

@vincentchu
Created March 9, 2011 19:31
Show Gist options
  • Save vincentchu/862805 to your computer and use it in GitHub Desktop.
Save vincentchu/862805 to your computer and use it in GitHub Desktop.
thread safety issues
require 'rubygems'
require 'redis'
def random_redis
100.times do
rand_key = "key_#{rand(100)}"
rand_val = rand
if (rand_val < 0.33)
puts "Setting #{rand_key} to #{rand_key}: #{$redis.set(rand_key, rand_key)}"
elsif (rand_val > 0.66)
puts "Getting #{rand_key}: #{$redis.get(rand_key)}"
else
puts "Deleting #{rand_key}: #{$redis.del(rand_key)}"
end
end
end
$redis = Redis.new
$redis.set "foo", "bar"
puts $redis.get "foo"
forks = []
10.times do
forks << fork {
srand
puts "Fork: pid #{Process.pid} - parent pid: #{Process.pid}"
random_redis
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment