Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update readme with ActiveRecord example

  • Loading branch information...
commit 54194c00c65b7cfb94dfb74a41e01829ebb57e55 1 parent 86feed2
Ilya Grigorik igrigorik authored

Showing 1 changed file with 27 additions and 17 deletions. Show diff stats Hide diff stats

  1. +27 17 README.md
44 README.md
Source Rendered
... ... @@ -1,28 +1,29 @@
1 1 # EM-Synchrony
2 2
3   -Collection of convenience classes and primitives to help untangle evented code, plus a number of patched EM clients to make them Fiber aware. To learn more, please see: [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers). Word of warning: even though Fibers have been backported to Ruby 1.8.x, these classes assume Ruby 1.9.x (if you're using fibers in production, you should be on 1.9.x anyway).
  3 +Collection of convenience classes and primitives to help untangle evented code, plus a number of patched EM clients to make them Fiber aware. To learn more, please see: [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers).
4 4
5   - * Fiber aware connection pool with sync/async query support
6   - * Fiber aware iterator to allow concurrency control & mixing of sync / async
  5 + * Fiber aware ConnectionPool with sync/async query support
  6 + * Fiber aware Iterator to allow concurrency control & mixing of sync / async
7 7 * Fiber aware async inline support: turns any async function into sync
8   - * Fiber aware multi-request interface for any callback enabled clients
  8 + * Fiber aware Multi-request interface for any callback enabled clients
9 9 * Fiber aware TCPSocket replacement, powered by EventMachine
10 10 * Fiber aware Thread, Mutex, ConditionVariable clases
11 11 * Fiber aware sleep
12 12
13 13 Supported clients:
14 14
15   - * em-http-request: .get, etc are synchronous, while .aget, etc are async
16   - * em-memcached & remcached: .get, etc, and .multi_* methods are synchronous
17   - * em-mongo: .find, .first are synchronous
18   - * mongoid: all functions synchronous, plus Rails compatability
19   - * em-redis: synchronous connect, .a{cmd} are async (see below, you should probably use the official redis.rb gem instead)
20   - * mysql2: .query is synchronyous, while .aquery is async (see specs)
  15 + * [mysql2](http://github.com/igrigorik/em-synchrony/blob/master/spec/mysql2_spec.rb): .query is synchronous, while .aquery is async (see specs)
  16 + * [activerecord](http://github.com/igrigorik/em-synchrony/blob/master/spec/activerecord_spec.rb): require synchrony/activerecord, set your AR adapter to em_mysql2 and you should be good to go
  17 + * [em-http-request](http://github.com/igrigorik/em-synchrony/blob/master/spec/http_spec.rb): .get, etc are synchronous, while .aget, etc are async
  18 + * [em-memcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/memcache_spec.rb) & [remcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/remcached_spec.rb): .get, etc, and .multi_* methods are synchronous
  19 + * [em-mongo](http://github.com/igrigorik/em-synchrony/blob/master/spec/em-mongo_spec.rb): .find, .first are synchronous
  20 + * [mongoid](http://github.com/igrigorik/em-synchrony/blob/master/spec/mongo_spec.rb): all functions synchronous, plus Rails compatibility
  21 + * em-jack: a[method]'s are async, and all regular jack method's are synchronous
21 22
22 23 Other clients with native Fiber support:
23 24
24 25 * redis: contains [synchrony code](https://github.com/ezmobius/redis-rb/blob/master/test/synchrony_driver.rb) right within the driver
25   - * synchrony also supports em-redis and em-hiredis (see specs), but unless you specifically need either of those, use the official redis gem
  26 + * synchrony also supports [em-redis](http://github.com/igrigorik/em-synchrony/blob/master/spec/redis_spec.rb) and em-hiredis (see specs), but unless you specifically need either of those, use the official redis gem
26 27
27 28 ## Fiber-aware Iterator: mixing sync / async code
28 29
@@ -31,6 +32,7 @@ Allows you to perform each, map, inject on a collection of any asynchronous task
31 32 ```ruby
32 33 require "em-synchrony"
33 34 require "em-synchrony/em-http"
  35 +
34 36 EM.synchrony do
35 37 concurrency = 2
36 38 urls = ['http://url.1.com', 'http://url2.com']
@@ -76,6 +78,7 @@ Allows you to create a pool of resources which are then shared by one or more fi
76 78 ```ruby
77 79 require "em-synchrony"
78 80 require "em-synchrony/mysql2"
  81 +
79 82 EventMachine.synchrony do
80 83 db = EventMachine::Synchrony::ConnectionPool.new(size: 2) do
81 84 Mysql2::EM::Client.new
@@ -147,14 +150,21 @@ EM.synchrony do
147 150 end
148 151 ```
149 152
150   -## Patched clients
  153 +## Async ActiveRecord:
151 154
152   -EM-Synchrony ships with a number of patched Eventmachine clients, which allow you to patch your asynchronous drivers on the fly to behave as if they were actually blocking clients:
  155 +Allows you to use async ActiveRecord within Rails and outside of Rails (see [async-rails](https://github.com/igrigorik/async-rails)). If you need to control the connection pool size, use [rack/fiber_pool](https://github.com/mperham/rack-fiber_pool/).
153 156
154   - * [em-http-request](http://github.com/igrigorik/em-synchrony/blob/master/spec/http_spec.rb)
155   - * [em-redis](http://github.com/igrigorik/em-synchrony/blob/master/spec/redis_spec.rb)
156   - * [em-memcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/memcache_spec.rb) & [remcached](http://github.com/igrigorik/em-synchrony/blob/master/spec/remcached_spec.rb)
157   - * [em-mongo](http://github.com/igrigorik/em-synchrony/blob/master/spec/em-mongo_spec.rb) & [mongoid](http://github.com/igrigorik/em-synchrony/blob/master/spec/mongo_spec.rb)
  157 +```ruby
  158 +require "em-synchrony"
  159 +require "em-synchrony/activerecord"
  160 +
  161 +ActiveRecord::Base.establish_connection(
  162 + :adapter => 'em_mysql2',
  163 + :database => 'widgets'
  164 +)
  165 +
  166 +result = Widget.all.to_a
  167 +```
158 168
159 169 # License
160 170

0 comments on commit 54194c0

Please sign in to comment.
Something went wrong with that request. Please try again.