Jay, On 3/4/06, Jay Strauss <[EMAIL PROTECTED]> wrote: > Hi, > > I'm not sure what's going on here. I have a simple script that reaches the > end (and prints out "here") but the script never ends and I have to ctrl-c > it.
Sometimes this happens when you use JNI. What happens it that in some cases there will be a Java thread started by the user's code that is not a daemon thread. That means that the Java Virtual Machine cannot terminate if such a thread exists. Even if the main (Perl) thread terminates, the other one is still hanging around doing whatever and prevents the JVM from terminating. I think (but I'm not sure) that even an exit(0) from Perl won't do it the thoses cases. I found that calling back to Java and doing a System.exit(0) from Java does the trick. See the quit() method here: http://search.cpan.org/~patl/Inline-Java-0.50/Java/Callback.pod#CALLBACK_LOOPS for an example. Patrick > > below is the simple script that never ends, and below that is the > Finance::InteractiveBrokers::TWS code that implements and wraps the > Inline::Java > > I think here must be some thread that is still active or some other resource > that is not terminated that's hanging my program, but I can't tell what it > is. > > Thanks for any help > Jay > > #!/usr/bin/perl > > use strict; > use warnings; > > package Local::Callback; > > sub new { > > my $class = shift; > return bless {}, $class; > } > > sub AUTOLOAD { > my ($self, @args) = @_; > our $AUTOLOAD; > print "$AUTOLOAD called with: ", join '^', @args, "\n"; > return; > } > > package main; > > use Finance::InteractiveBrokers::TWS; > > my $cb = Local::Callback->new(); > my $tws = Finance::InteractiveBrokers::TWS->new(callback=>$cb); > > my $client_socket = $tws->get_EClientSocket(); > my $api = $tws->get_api(); > > my $host = "pt"; > my $port = 7496; > my $client_id = $$; > > $api->OpenCallbackStream(); > > while (! $tws->get_EClientSocket->isConnected()) { > $client_socket->eConnect($host, $port, $client_id); > } > > while ((my $rc = $api->WaitForCallback(1)) > -1){ > > if ($rc > 0){ > $api->ProcessNextCallback() ; > last if ! $tws->get_EClientSocket->isConnected(); > } > else { > # A timeout has occured after, in this case, 5 secs. > print "5 seconds have passed, still waiting for callback...\n" ; > $client_socket->eDisconnect(); > } > } > > print "here\n"; > > > A link to Finance::InteractiveBrokers::TWS > http://search.cpan.org/src/JSTRAUSS/Finance-InteractiveBrokers-TWS-v0.0.5/lib/Finance/InteractiveBrokers/TWS.pm > > -- ===================== Patrick LeBoutillier Laval, Québec, Canada