In Perl, how can I make a non-blocking system call?

In Perl, without using a library Thread

, what's the easiest way to call a system call so it doesn't block? Can you do this while avoiding it fork()

?

EDIT Clarification. I want to avoid the explicit and messy fork call.

+2


source to share


2 answers


Do you mean this?

system('my_command_which_will_not_block &');

      



As Chris Kloberdantz points out, this will call fork () implicitly - there is no other way for Perl to do this; especially if you want the perl interpreter to keep running while the command is being executed.

The character &

in the command is a shell metacharacter - perl sees this and passes the argument system()

to the shell (usually bash) for execution, rather than launching it directly with a execv()

call. &

tells bash to fork again, run the command in the background and exit immediately, returning control to perl while the command continues to run.

+9


source


The above message says "there is no other way to execute perl" which is not correct.

Since you mentioned deleting a file, take a look at IO :: AIO . This makes system calls on a different thread (POSIX thread, not Perl pseudothread); you schedule a request with aio_rmtree

, and when it's done, the module will call a function in your program. In the meantime, your program can do whatever it wants.



Doing things on a different POSIX thread is actually a generally useful technique. (Special hacked version). Coro uses it to unload coroutines (time slice), and EV :: Loop :: Async uses it to deliver event notifications even if Perl is doing something other than waiting for events.

+4


source







All Articles