in reply to Re: using online translation engines with perl
in thread using online translation engines with perl
Thanks for elaborating on utf8 and suggesting on a way forward with my questions. As is frequently the case, the monastery has another thread going to discuss the same issue: Can we write multi-threaded scripts ?. I looked at the reference that localshop posted and found code that uses perl's native language to thread. I would prefer not to have to use a module just for this little task. I must say, however that I fall short on my first attempt in some ways. Output then source:
$ ./2.fork.pl this is task 1 with pid 0 Waiting for child processes.. this is task 2 with pid 0 this is task 3 with pid 0 44 bing's translation is unctuous hypocrisy flowing from the tube елейным лицемерие течет из трубки English -> Русский Child with PID=23085 finished.. 43 42 41 slept 5 40 39 38 ERROR Oops! Something went wrong and I can't translate it for you :( yandex's translation is Child with PID=23086 finished.. 37 36 slept 10 35 google's translation is unctuous hypocrisy flowing from the tube неприступное лицемерие, вытекающее из трубки (nepristupnoye litsemeriye, vytekayushcheye iz trubki) Translations of unctuous hypocrisy flowing from the tube English -> Русский unctuous hypocrisy flowing from the tube неприступное лицемерие, вытекающее из трубки, елейное лицемерие течет из трубки Child with PID=23087 finished.. Done. $ cat 2.fork.pl #!/usr/bin/perl -w use 5.011; my $string = "unctuous hypocrisy flowing from the tube"; my $pid1 = fork(); if ( $pid1 == 0 ) { # Task 1 say "this is task 1 with pid $pid1"; my $command = "trans :ru -e bing \"$string\" >1.bing.txt"; system("$command"); say "bing's translation is "; system("cat 1.bing.txt"); exit 0; } my $pid2 = fork(); if ( $pid2 == 0 ) { # Task 2 say "this is task 2 with pid $pid2"; sleep 5; say "slept 5"; system("trans :ru -e yandex \"$string\" >1.yandex.txt"); say "yandex's translation is "; system("cat 1.yandex.txt"); exit 0; } my $pid3 = fork(); if ( $pid3 == 0 ) { # Task 3 say "this is task 3 with pid $pid3"; sleep 10; say "slept 10"; system("trans :ru -e google \"$string\" >1.google.txt"); say "google's translation is "; system("cat 1.google.txt"); exit 0; } say "Waiting for child processes.."; my $counter = 45; local $SIG{ALRM} = sub { say --$counter; alarm 1; }; alarm 1; while ((my $pid = wait) != -1) { say "Child with PID=$pid finished.."; } alarm 0; say "Done."; __END__
Even with all the say statements dropped in, I find execution hard to follow. I'm baffled that pid's are zero within a block, but not so when they finish. What alarm truly does here is unclear. Finally, there is no code to kill pid's when the timer reaches zero.
I'm also fishing for code that would do this within the following loop:
print "Get other translations(y/n)?: "; my $prompt = <STDIN>; chomp $prompt; if ( $prompt eq ( "y" | "Y" ) ) { my @translators = qw /yandex bing/; for my $remote (@translators) { my $trans_munge = path( $vars{translations}, "$remote." . $munge + ); ## use trans shell say "getting translation from $remote"; system("trans :$lang -e $remote file://$in_path >$trans_munge"); }
Again, thanks for the very helpful comments.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: using online translation engines with perl
by Anonymous Monk on Nov 07, 2018 at 07:25 UTC |