Multiple exec calls on the same connection from threads

12 views
Skip to first unread message

Refael Dakar

unread,
Jul 11, 2017, 1:45:06 PM7/11/17
to ruby-pg
Hey,

I'm trying to run multiple exec calls from different threads asynchronous and I'm getting memory errors. I had to go back to opening and closing a connection for every thread which makes things work a lot slower.

Any ideas how this can be done ?

Thanks!

Refael

Lars Kanis

unread,
Jul 11, 2017, 2:10:24 PM7/11/17
to rub...@googlegroups.com


On Jul 11, 2017 19:45, "Refael Dakar" <refae...@gmail.com> wrote:
Hey,

I'm trying to run multiple exec calls from different threads asynchronous and I'm getting memory errors.

This is normal. Consider, there's a single TCP connection as communication channel, which is filled and read out by simultaneous threads. Command and result sequences get mixed to a great garbage.

I had to go back to opening and closing a connection for every thread which makes things work a lot slower.

Unfortunately that's the way to go.


Any ideas how this can be done ?

It is possible to use a single connection object from different threads - but only if you ensue, that any handover to a different thread is only done, when the connection is in idle state.

--
Regards,
Lars

Refael Dakar

unread,
Jul 12, 2017, 2:16:17 AM7/12/17
to rub...@googlegroups.com
Got it. Thanks man!

--
You received this message because you are subscribed to a topic in the Google Groups "ruby-pg" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-pg/7NVpUYgay58/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-pg+unsubscribe@googlegroups.com.
To post to this group, send email to rub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-pg/CAHXCYb1j%3D81TWJH69ZsRoE0dQxYE91KT%2BKhABFPDoUCG2BCG5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Refael Dakar


Robert Klemme

unread,
Jul 12, 2017, 2:35:45 AM7/12/17
to rub...@googlegroups.com
On Wed, Jul 12, 2017 at 8:16 AM, Refael Dakar <refae...@gmail.com> wrote:
On Tue, Jul 11, 2017 at 9:10 PM, Lars Kanis <la...@greiz-reinsdorf.de> wrote:
On Jul 11, 2017 19:45, "Refael Dakar" <refae...@gmail.com> wrote:
 
I had to go back to opening and closing a connection for every thread which makes things work a lot slower.

Unfortunately that's the way to go.

Well, one can of course use a connection pool which avoids the repeated opening and closing of connections. Or use one connection per thread and close it only when the thread terminates. That all depends on the circumstances.

Cheers

robert

-- 
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

Refael Dakar

unread,
Jul 12, 2017, 2:49:36 AM7/12/17
to rub...@googlegroups.com
Do you have a connection pool code I can use?

--
You received this message because you are subscribed to a topic in the Google Groups "ruby-pg" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-pg/7NVpUYgay58/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-pg+unsubscribe@googlegroups.com.
To post to this group, send email to rub...@googlegroups.com.

Robert Klemme

unread,
Jul 12, 2017, 3:17:52 AM7/12/17
to rub...@googlegroups.com
On Wed, Jul 12, 2017 at 8:49 AM, Refael Dakar <refae...@gmail.com> wrote:
> Do you have a connection pool code I can use?

Search: https://duckduckgo.com/?q=ruby+gem+pool&t=canonical&ia=software
Hit: https://rubygems.org/gems/connection_pool/versions/2.2.1

Refael Dakar

unread,
Jul 12, 2017, 5:17:55 AM7/12/17
to ruby-pg
perfect. thanks!
Reply all
Reply to author
Forward
0 new messages