I'm trying to get started with the concept of multiple threads. Basically what I'm trying to do is run two system commands simultaneously. I'm not sure if it matters, but the command in question is mysqldump. I have no idea how to do this. Can someone point me in the right direction? I would like to run 2 of the following subroutines simultaneously until all databases have been dumped (please note that this is a snippet):
for my $db (keys %{$db_ds}) { if (!@{$db_ds->{$db}} && !$opts{database}) { ### Our data stuctur says there is no native mysql tables. ### So we skip this database. next; } $dbh->do(qq{use $db}); ### The table list in our data structure is empty. The database ### option has been passed, so look them up. if (!@{$db_ds->{$db}}) { $queries{get_table_names}->execute(); while (my $rec = $queries{get_table_names}->fetchrow_hashref() +) { push @{$db_ds->{$db}}, $rec->{Name}; } debug(qq{Got table list for $db\n}); } ### This block makes sure we can actually access the tables. my @valid_tables; for my $table (@{$db_ds->{$db}}) { $queries{verify_table_name}->execute($table); if ($queries{verify_table_name}->rows()) { my ($rec) = $queries{verify_table_name}->fetchrow_array(); push @valid_tables, $rec; } else { debug(qq{There was a problem finding database/table $db $t +able\n}); } } ### Make sure we aren't running out of filesystem space. chk_space($max_disk_space); my $current_date = strftime("%Y-%m-%d", localtime); my $current_time = strftime("%H:%M:%S", localtime); my $dumpfile = '/backups/table_dumps/' . $db . '.' . $current_date . '.' . $current_time; my $valid_table_list = join ' ', @valid_tables; my $cmd = 'mysqldump -q --single-transaction --complete-insert -e' . ' ' . $db . ' ' . $valid_table_list . $bar_cmd . ' |'; debug(qq{Beginning dump of database: $db\n}); debug(qq{Running command: $cmd\n}); debug(qq{Logging to: $dumpfile\n}); open MYSQLDUMP, "$cmd" or die $!; open DUMPFILE, ">$dumpfile" or die $!; my $total_bytes = 0; while (my $bytes_read = read(MYSQLDUMP, my $buffer, 4096)) { $total_bytes += $bytes_read; print DUMPFILE $buffer; } close DUMPFILE; debug(qq{Finished dumping $db\n\n}); }

In reply to Using simultaneous threads by mhearse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.