in reply to Thread very slow

Maybe tied the variables in your Tk GUI, so they automatically update whenever they get changed from the GUI? And it seems that passing these variables into the thread queue did not dissociate them, which seems weird to me.

Personally, I would only pass values through queues, that is, copy the values out. Also, I wouldn't pass multiple different values to the thread but pass in all values for one job in one go:

use Data::Dumper; my $job = {}; for my $field (qw( lri pm gp )) { $job->{ $field } = { %$gen_data{ $field } }; warn "Processing " . Dumper $job; sleep 10; }; warn "Enqueuing " . Dumper $job; $q->enqueue( $job ); $q->enqueue( undef ); # tell the thread that we're done and it should +quit ... # In the thread while (my $job = $q->dequeue) { };

Replies are listed 'Best First'.
Re^2: Thread very slow
by Dirk80 (Pilgrim) on Jul 12, 2010 at 13:22 UTC

    Thanks. Your advice with Data::Dumper helped me a lot. Your code version was not the final solution because the thread was still slow. But it helped me to find the solution. Now the thread is running fast.

    Here the code of the main task:

    $Data::Dumper::Varname = "gen_data_str"; $gen_data_str1 = Dumper($ref_gen_data); $q->enqueue( $gen_data_str1 ); $q->enqueue( undef );

    Here the code within the worker thread:

    my %gen_data; while( my $gen_data_str1 = $q->dequeue ) { %gen_data = %{ eval $gen_data_str1 }; };

      I think there must be still something going wrong with your serialization, as Thread::Queue basically does what you do, except it's using the (slightly faster in most cases) Storable::nstore to serialize the data for the queue.

      As you haven't shown the code where you construct %gen_data and the values you construct them from, it's hard to guess. If you can construct an example without Tk that exhibits the behaviour, that would be convenient. If you can post (a small excerpt of) how you populate %gen_data, that might also lead to more clue as to what really goes wrong.