How can I call a subroutine / function and not wait for it to return?

B

bayxarea-usenet

How can I call a subroutine / function that is either local to the .pl
or from a module and not wait for it to return - rather continue on ...

I have a routine that is already wrapped in a function I wrote in a
module I 'use'. My program needs to call it and then I want to enter a
loop to monitor an output file at the OS that will be created from this
function (interacts with other software) - but I don't know how to
enter the loop after the call to the subroutine since Perl will wait
there at that line until it returns....

My monitor loop is necessary so I can update the GUI and time out after
X seconds and provide feedback that the process took longer than the
timer.

Any clues?

Thanks,

John
 
A

Anno Siegel

How can I call a subroutine / function that is either local to the .pl
or from a module and not wait for it to return - rather continue on ...

I have a routine that is already wrapped in a function I wrote in a
module I 'use'. My program needs to call it and then I want to enter a
loop to monitor an output file at the OS that will be created from this
function (interacts with other software) - but I don't know how to
enter the loop after the call to the subroutine since Perl will wait
there at that line until it returns....

My monitor loop is necessary so I can update the GUI and time out after
X seconds and provide feedback that the process took longer than the
timer.

perldoc perlipc
perldoc -f fork

Anno
 
B

bayxarea-usenet

This is not another process or system call - this is a subroutine - so
IPC and fork are not relavent (AFAIK)

Imagine:

my_sub(args);

$timer = 10;

while($timer) {

# check to see if flag is found in file

} # end while


sub my_sub {

# do something
# write output to a file --- blah blah blah

} # end sub



I want to call my_sub and immediately enter the while loop.
Thanks,

John
 
A

Anno Siegel

This is not another process or system call - this is a subroutine - so

What is "this"? Please show some consideration and provide context.
IPC and fork are not relavent (AFAIK)

Sure they are.
Imagine:

my_sub(args);

$timer = 10;

while($timer) {

How is $timer ever decremented?
# check to see if flag is found in file

} # end while


sub my_sub {

# do something
# write output to a file --- blah blah blah

} # end sub



I want to call my_sub and immediately enter the while loop.

Yes. So fork a process and let it run the sub. Use IPC to talk back to
the parent process. Your best bet may be for the sub not to write to a
file but to STDOUT and use open( ..., '-|') for the fork, so the main
process can read the output directly.

Then again, you may be looking for threads. No advice from me there.
Until I'm running a multi-processor and need the extra power, I'm not
touching threads.

Anno
 
X

xhoster

How can I call a subroutine / function that is either local to the .pl
or from a module and not wait for it to return - rather continue on ...

I have a routine that is already wrapped in a function I wrote in a
module I 'use'. My program needs to call it and then I want to enter a
loop to monitor an output file at the OS that will be created from this
function (interacts with other software) - but I don't know how to
enter the loop after the call to the subroutine since Perl will wait
there at that line until it returns....

perldoc -q background

Xho
 
X

xhoster

This is not another process or system call - this is a subroutine - so
IPC and fork are not relavent (AFAIK)

So, you want to solve a certain type of problem in Perl, but you do not
want to use the tools which perl provides for solving that type of problem?



Xho
 
B

bayxarea-usenet

Uh ... no.. it is NOT that I don't want to use the tools .. it is that
AFAIK (as far as I know) these are not relevant

What I know -

IPC - will allow me to call communicate with another process - ok -
great - but this isn't another process.
fork - will allow me to spawn the current process as a seperate child -
ok - great but this isn't what I thought I would need - perhaps it is -
hence the AFAIK in my reply!

after reading the post from Anno regarding fork - I suppose I could do
it that way - I was just hoping there was a way to handle it without
starting an entire new version of the original code - this is a large
program and I only want to exectute one tiny subroutine

John
 
J

Jay Tilton

Usenet tradition is to supply context for your comments by quoting relevant
parts of the article you are replying to. Your audience of clpm readers is
dramatically less eager to assist those who do not observe this tradition.

See http://mail.augustmail.com/~tadmc/clpmisc.shtml for good advices.


(e-mail address removed) wrote:

: Uh ... no.. it is NOT that I don't want to use the tools .. it is that
: AFAIK (as far as I know) these are not relevant

You want a portion of the program to perform its assigned task while the
larger portion of the program carries on its own processing. You exactly
describe the type of problem that forking was designed to solve.

: IPC - will allow me to call communicate with another process - ok -
: great - but this isn't another process.

It needs to be.

: fork - will allow me to spawn the current process as a seperate child -
: ok - great but this isn't what I thought I would need - perhaps it is

It is.
 
B

Brian McCauley

Jay said:
(e-mail address removed) wrote:

: Uh ... no.. it is NOT that I don't want to use the tools .. it is that
: AFAIK (as far as I know) these are not relevant

You want a portion of the program to perform its assigned task while the
larger portion of the program carries on its own processing. You exactly
describe the type of problem that forking was designed to solve.

: IPC - will allow me to call communicate with another process - ok -
: great - but this isn't another process.

It needs to be.

No, it could be a thread. But given the way threads are implemented in
Perl a fork would be better on Unix.
 
X

xhoster

Uh ... no.. it is NOT that I don't want to use the tools .. it is that
AFAIK (as far as I know) these are not relevant

But people who know more than you on the subject keep telling you that they
are relevant!

What I know -

IPC - will allow me to call communicate with another process - ok -
great - but this isn't another process.

If you want it to run independently of the main program flow, the easiest
way is to make it another process (or another thread, but Perl threading
is not something I recommend). You could rewrite this simple sub into a
very complex event-loop or polling type of thing, but that would make an
awful mess of the code.
fork - will allow me to spawn the current process as a seperate child -
ok - great but this isn't what I thought I would need - perhaps it is -
hence the AFAIK in my reply!

after reading the post from Anno regarding fork - I suppose I could do
it that way -

If you want a module that will hide a lot of the mess from you, you may
be able to "use forks" if you are on unix, or "use threads" if you are on
Windows. But these might not work for you, as there does not appear to
be a non-blocking "join" method.

I was just hoping there was a way to handle it without
starting an entire new version of the original code - this is a large
program and I only want to exectute one tiny subroutine

It doesn't really matter how big the code is. Fork is very efficient about
that. Where you might run into trouble is if the data structures are very
large. The finalization of the data structures by the child when it exits
can take a long time, so in that case the child should perhaps use
POSIX::_exit.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top