Waiting from the skull

I am working on a script that should run the Expect process periodically (every 5 minutes) to do some work. Below is the code that I have that starts the Expect process and does some work. The main script process does any other work at all times, for example, it can wait for user input, because of this I call this function "spawn_expect" in the thread, which continues to call it every 5 minutes, but the problem is that Expect does not work as expected.

If, however, I replace the thread with another process, that is, if I fork and let one process take care of Expect spawning and the other process do the main work of the script (for example, wait at the prompt), then Expect works fine.

My question is, is it possible to start the Pending thread? Should I resort to using a process to do this job? Thank!

sub spawn_expect {
    my $expect = Expect->spawn($release_config{kinit_exec});
    my $position = $expect->expect(10,
                    [qr/Password.*: /, sub {my $fh = shift; print $fh "password\n";}],
                    [timeout => sub {print "Timed out";}]);
    # if this function is run via a process, $position is defined, if it is run via a thread, it is not defined                         
    ...
 }
+4
source share
1 answer

Create Expectan object in advance (not inside the thread) and pass it to the subject

my $exp = Expect->spawn( ... );
$exp->raw_pty(1);
$exp->log_stdout(0);

my ($thr) = threads->create(\&login, $exp);
my @res = $thr->join();    
# ...

sub login {
    my $exp = shift;
    my $position = $exp->expect( ... );
    # ...
}

I tested several threads where one uses Expectwith a custom test script and returns the script output to the main thread. Let me know if I should publish these (short) programs.

When an object Expectis created inside a stream, it does not work either. I assume that in this case it cannot configure its pty as usual.

, fork .

+1

Source: https://habr.com/ru/post/1676770/


All Articles