我看到了一些奇怪的问题,我正在试图理解,这个问题的第一部分是试图了解数据库池是如何在Celluloid和Celluloid/ZMQ中工作的。
数据库池
1)螺纹。
5. "#{ActiveRecord::Base.connection.execute('SELECT版本();‘).first}--#{ActiveRecord::Base.connection_id}--“睡眠5结束睡眠”的时间
注意,我是输出(Ting)线程使用的connection id。O/p
{"version"=>"PostgreSQL 9.4.5 .."} --- 70115833371600 <- connection id
{"version"=>"PostgreSQL 9.4.5 .."} --- 70115833339020 <- connection id
{"version"=>"PostgreSQL 9.4.5 .."} --- 70115833290000 ...
{"version"=>"PostgreSQL 9.4.5 .."} --- 70115833282580 ...
{"version"=>"PostgreSQL 9.4.5 .."} --- 70115833251100 ...如您所见,执行SQL语句的每个线程都返回不同的连接id。SELECT version();
根据我的假设,上面的假设是完全正确的。
2)纤维素
"#{ActiveRecord::Base.connection.execute('SELECT版本();‘).first}-#{ActiveRecord::Base.connection_id}- #{ActiveRecord::Base.connection_id}“睡眠5结束5次Sender.new.async.run结束
O/p
{"version"=>"PostgreSQL 9.4.5 .."} --- 70120202935840 <- connection id
{"version"=>"PostgreSQL 9.4.5 .."} --- 70120202902760 <- connection id
{"version"=>"PostgreSQL 9.4.5 .."} --- 70120186634700 ...
{"version"=>"PostgreSQL 9.4.5 .."} --- 70120186602720 ...
{"version"=>"PostgreSQL 9.4.5 .."} --- 70120186570720 ...同样,如预期的那样,每个--每个细胞体--都会产生一个新的连接id。完全有效。
3) Celluloud/ZMQ
类发件人包括单元格::ZMQ def initialize() @socket = Socket::Push.new @socket.connect(‘ipc://tmp/qs11 11’)端def写@socket.send('Hello')
class Receiver include Celluloid::ZMQ def initialize() @socket = Socket::Pull.new @socket.bind('ipc:///tmp/qs11') end def run loop do async.handle\_message @socket.read end end def handle\_message(message) puts "#{ActiveRecord::Base.connection.execute('SELECT version();').first} --- #{ActiveRecord::Base.connection\_id}" sleep 10 end end Receiver.new.async.run
现在有趣的是。在执行的时候。
5. Sender.new.async.write结束的时间
我看到下面的输出。
{"version"=>"PostgreSQL 9.4.5 ..."} --- 70299372892280 <- connection id
{"version"=>"PostgreSQL 9.4.5 ..."} --- 70299372892280
{"version"=>"PostgreSQL 9.4.5 ..."} --- 70299372892280
{"version"=>"PostgreSQL 9.4.5 ..."} --- 70299372892280
{"version"=>"PostgreSQL 9.4.5 ..."} --- 70299372892280所有查询都使用相同的连接id。
这就是我的问题..。
为什么Celluloid/ZMQ使用相同的连接id。理想情况下,它应该对每个async调用使用不同的方法。
发布于 2016-04-14 11:58:47
因为一个Receiver为多个Sender实例提供服务。
注意:Sender在Celluloid::ZMQ示例中不处理记录本身。许多人把它们送给一个接收者。因此,您将看到Receiver只使用该连接,这也是给定的有效行为。
如果您希望它是不同的--您将需要第三种类型的参与者,它在其生命周期内纯粹处理单个数据库连接。这是你的前两种方法和最后一种方法的结合。
https://stackoverflow.com/questions/36620856
复制相似问题