I am developing a small GUI application using jruby. It will be used
for testing API calls to my company's server. I would like my
application to execute server calls in a non-blocking way. In C++ I do
this by creating a worker thread that executes the call and then
invokes a postback to the main thread (similar to Java's InvokeLater).
How do I do this in Ruby?
My simplified code looks something like this:
def get_users
Thread.new(server, successCallback, errorCallback) do |server, scb,
ecb|
res = Net::HTTP.post_form(server, { "action" => "getUsers"})
if res.body
scb.call(res.body) # => this should be wrapped in some sort of
InvokeLater ?
else
ecb.call("getUsers failed") # => this too
end
end
end
I would prefer to use the Ruby standard library over the Java library.
Eager to learn,
Francis
|
|
0
|
|
|
|
Reply
|
francis.rammeloo (37)
|
10/20/2008 7:42:54 PM |
|
On 20.10.2008 21:42, Dolazy wrote:
> I am developing a small GUI application using jruby. It will be used
> for testing API calls to my company's server. I would like my
> application to execute server calls in a non-blocking way.
What does the rest of the application do in the meantime?
> How do I do this in Ruby?
>
> My simplified code looks something like this:
>
> def get_users
> Thread.new(server, successCallback, errorCallback) do |server, scb,
> ecb|
> res = Net::HTTP.post_form(server, { "action" => "getUsers"})
> if res.body
> scb.call(res.body) # => this should be wrapped in some sort of
> InvokeLater ?
> else
> ecb.call("getUsers failed") # => this too
> end
> end
> end
>
> I would prefer to use the Ruby standard library over the Java library.
Not sure what exactly you mean by "InvokeLater". Do you want to pass
the result of method calls back to the calling thread? Or do you want
to asynchronously invoke callbacks?
One thing which might be useful for you is Thread#value which joins a
thread and returns the last value:
robert@fussel ~
$ ruby -e 'puts Time.now;t=Thread.new {sleep 1; 123};puts t.value, Time.now'
Mon Oct 20 22:28:28 +0200 2008
123
Mon Oct 20 22:28:29 +0200 2008
robert@fussel ~
$
Another option is to set up a Queue for feedback which is queried from
the main thread. It all depends on what you application does or what
you want to achieve. Can you give more detail?
Kind regards
robert
|
|
0
|
|
|
|
Reply
|
shortcutter (5765)
|
10/20/2008 8:29:51 PM
|
|
On 20 okt, 22:29, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 20.10.2008 21:42, Dolazy wrote:
>
> > I am developing a small GUI application using jruby. It will be used
> > for testing API calls to my company's server. I would like my
> > application to execute server calls in a non-blocking way.
>
> What does the rest of the application do in the meantime?
Anything that the user is doing at that time. I want to avoid freezing
the GUI.
> Not sure what exactly you mean by "InvokeLater". =A0Do you want to pass
> the result of method calls back to the calling thread?
Yes, that's exactly what I have in mind.
> Another option is to set up a Queue for feedback which is queried from
> the main thread. =A0It all depends on what you application does or what
> you want to achieve. =A0Can you give more detail?
For example, one of the first calls will be the login method. In a
worker thread I want to send the login message, receive the response,
and then notify the main thread of either a successful or failed
login, by means of invoking the corresponding callback.
|
|
0
|
|
|
|
Reply
|
francis.rammeloo (37)
|
10/20/2008 9:14:23 PM
|
|
Dolazy wrote:
>=20
> On 20 okt, 22:29, Robert Klemme <shortcut...@googlemail.com> wrote:
>> On 20.10.2008 21:42, Dolazy wrote:
>>
>> > I am developing a small GUI application using jruby. It will be used
>> > for testing API calls to my company's server. I would like my
>> > application to execute server calls in a non-blocking way.
>>
>> What does the rest of the application do in the meantime?
> Anything that the user is doing at that time. I want to avoid freezing
> the GUI.
>=20
>> Not sure what exactly you mean by "InvokeLater". =C2=A0Do you want to pa=
ss
>> the result of method calls back to the calling thread?
>=20
> Yes, that's exactly what I have in mind.
>=20
>> Another option is to set up a Queue for feedback which is queried from
>> the main thread. =C2=A0It all depends on what you application does or wh=
at
>> you want to achieve. =C2=A0Can you give more detail?
>=20
> For example, one of the first calls will be the login method. In a
> worker thread I want to send the login message, receive the response,
> and then notify the main thread of either a successful or failed
> login, by means of invoking the corresponding callback.
>=20
I'm going to out on a limb and assume you're using Swing here. To keep the
GUI painting properly and new events being processed you have to make sure
the EDT (event dispatch thread) does not block. So in your event handler
you need to spin off a new thread and return as fast as possible to prevent
"laggyness" in the UI. This does mean you need some asynchronous way of
getting your response, a queue is probably your best bet here (plus it's
thread safe). In Monkeybars, we used a library named Foxtrot (Java lib)
that processed GUI redraw events (but not new actions) while an action was
being performed. For short-lived processes this worked great and prevented
the mess of having to get data back from an asynchronous thread.
David Koontz
--=20
View this message in context: http://www.nabble.com/Threading-best-practice=
s--tp20077301p20081656.html
Sent from the ruby-talk mailing list archive at Nabble.com.
|
|
0
|
|
|
|
Reply
|
david6751 (14)
|
10/21/2008 12:55:14 AM
|
|
> I'm going to out on a limb and assume you're using Swing here. =A0To keep=
the
> GUI painting properly and new events being processed you have to make sur=
e
> the EDT (event dispatch thread) does not block. =A0So in your event handl=
er
> you need to spin off a new thread and return as fast as possible to preve=
nt
> "laggyness" in the UI. =A0This does mean you need some asynchronous way o=
f
> getting your response, a queue is probably your best bet here (plus it's
> thread safe). =A0InMonkeybars, we used a library named Foxtrot (Java lib)
> that processed GUI redraw events (but not new actions) while an action wa=
s
> being performed. =A0For short-lived processes this worked great and preve=
nted
> the mess of having to get data back from an asynchronous thread.
>
> David Koontz
>
Yeah I started using MonkeyBars. I find it a very interesting project,
but complicated to learn (may also since the tutorial videos don't
seem to work ;) I'm gonna stick to it though because it combines so
much great stuff (Ruby style, Swing power, "neo"-MVC, neat folder
structure, deployment, ...)
|
|
0
|
|
|
|
Reply
|
francis.rammeloo (37)
|
10/21/2008 9:43:14 PM
|
|
Dolazy wrote:
>
> Yeah I started using MonkeyBars. I find it a very interesting project,
> but complicated to learn (may also since the tutorial videos don't
> seem to work ;) I'm gonna stick to it though because it combines so
> much great stuff (Ruby style, Swing power, "neo"-MVC, neat folder
> structure, deployment, ...)
>
Much apologies for the tutorial videos not working. We did a bit of server
shuffling a while back and apparently I never got the video files back
online. I have fixed this and the last few files will be online within a
half hour. Hopefully that will help a lot with the "getting started"
business.
David Koontz
--
View this message in context: http://www.nabble.com/Threading-best-practices--tp20077301p20100946.html
Sent from the ruby-talk mailing list archive at Nabble.com.
|
|
0
|
|
|
|
Reply
|
david6751 (14)
|
10/21/2008 10:39:11 PM
|
|
|
5 Replies
31 Views
(page loaded in 0.094 seconds)
Similiar Articles: Getting a hConsoleOutput handle - comp.os.ms-windows.programmer ...... practices isn't helpful, it will only get you ignored or flamed.</p> </blockquote> <p>So why then did you deliberately violate the best practice of moving a thread to ... how to exit gracefully - comp.lang.java.programmer... done anything like set up database connections or launch forty-two worker threads ... heard that using > > System.exit(somenumber) is frowned upon.Which is the best practice? Adding threads to Hole Wizard - comp.cad.solidworksHow do you add Custom thread sizes to the Hole wizard? example high ... Hole wizard and circular pattern best practice ? - comp.cad ... In an assembly, edit part ... How to check if a property is writable? - comp.lang.javascript ...... URL: http://groups.google.com/group/comp.lang.javascript/tree/browse_frm/thread ... javascript FAQ - http://jibbering.com/faq & newsgroup weekly Javascript Best Practices ... nscd recommended settings - comp.unix.solarisSolaris Operating System: nscd, door_call, multi-threading ... nscd strategy files ... Linux can run nscd or BIND or dnsmasq as the name ... 25 PHP Security Best Practices ... Are we Top Posting now? - comp.dspIn view of past practice, I find it just a bit confusing. ... should go ahead and top-post as the next-best ... When commenting on a thread as a whole, top posting ... Waiting for ActiveX object to have initialized - comp.lang ...... javascript FAQ - http://jibbering.com/faq & newsgroup weekly Javascript Best Practices ... of the loading of the activex... not to mention not blocking the calling thread ... Question about these new x64 servers - comp.unix.solaris ...Granted, you should be running Solaris 10 for best ... 32 bit Perl with largefiles, 32 bit Perl with threading ... Best Practices Question: Upgrading Windows x32 Server to x64 ... Single best NTP status indicator of clock accuracy? - comp ...This thread has been dormant for more than 7 months! Ulrich Windl wrote: > kjans ... clock ... temperature or voltage changes, it takes NTP some time ... NTP - best practice ... Custom Properties linking to filename. - comp.cad.solidworks ...Hi: There was a thread regarding this subject back in May. I tried to reply to ... best practices for naming configurations? - comp.cad.solidworks ... I then make the ... Managed Threading Best PracticesMultithreading requires careful programming. For most tasks, you can reduce complexity by queuing requests for execution by thread pool threads. This topic addresses ... Videos - Threading in .NET* - Best Practices - 1 of 7 seriesThis series was designed for current .NET developers who have an interest in threading but have not had extensive experience in threading nor how threading enables ... 7/4/2012 4:51:39 AM
|