简体   繁体   中英

Fiber within EM:Connection (em-synchrony)

can anybody explain me why the Redis (redis-rb) synchrony driver works directly under EM.synchrony block but doesn't within EM:Connection?

Considering following example

    EM.synchrony do
        redis = Redis.new(:path => "/usr/local/var/redis.sock")

        id = redis.incr "local:id_counter"
        puts id 

        EM.start_server('0.0.0.0', 9999) do |c|
            def c.receive_data(data)
                redis = Redis.new(:path => "/usr/local/var/redis.sock")
                puts redis.incr "local:id_counter"
            end
        end

    end

I'm getting

can't yield from root fiber (FiberError)

when using within receive_data . From reading source code for both EventMachine and em-synchrony I can't figure out what's the difference.

Thanks!

PS: Obvious workaround is to wrap the redis code within EventMachine::Synchrony.next_tick as hinted at issue #59 , but given the EM.synchrony I would expect to already have the call wrapped within Fiber...

PPS: same applies for using EM::Synchrony::Iterator

You're doing some rather tricky here.. You're providing a block to start_server, which effectively creates an "anonymous" connection class and executes your block within the post_init method of that class. Then within that class you're defining an instance method.

The thing to keep in mind is: when the reactor executes a callback, or a method like receive_data, that happens on the main thread (and within root fiber), which is why you're seeing this exception. To work around this, you need to wrap each callback to be executed within a Fiber (ex, see Synchrony.add_(periodic)_timer methods).

To address your actual exception: wrap the execution of receive_data within a Fiber. The outer EM.synchrony {} won't do anything for callbacks which are scheduled later by the reactor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM