NAME

Padre::TaskManager - Padre Background Task Scheduler

SYNOPSIS

require Padre::Task::Foo;
my $task = Padre::Task::Foo->new(some => 'data');
$task->schedule(); # handed off to the task manager

DESCRIPTION

Padre uses threads for asynchroneous background operations which may take so long that they would make the GUI unresponsive if run in the main (GUI) thread.

This class implements a pool of a configurable number of re-usable worker threads. Re-using threads is necessary as the overhead of spawning threads is high. Additional threads are spawned if many background tasks are scheduled for execution. When the load goes down, the number of extra threads is (slowly!) reduced down to the default.

CLASS METHODS

new

The constructor returns a Padre::TaskManager object. At the moment, Padre::TaskManager is a singleton. An object is instantiated when the editor object is created.

Optional parameters:

min_no_workers / max_no_workers

Set the minimum and maximum number of worker threads to spawn. Default: 1 to 3

The first workers are spawned lazily: I.e. only when the first task is being scheduled.

use_threads

TODO: This is disabled for now since we need Wx 0.89 for stable threading.

Disable for profiling runs. In the degraded, threadless mode, all tasks are run in the main thread. Default: 1 (use threads)

reap_interval

The number of milliseconds to wait before checking for dead worker threads. Default: 15000ms

INSTANCE METHODS

schedule

Given a Padre::Task instance (or rather an instance of a subclass), schedule that task for execution in a worker thread. If you call the schedule method of the task object, it will proxy to this method for convenience.

setup_workers

Create more workers if necessary. Called by reap which is called regularly by the reap timer, so users don't typically need to call this.

reap

Check for worker threads that have exited and can be joined. If there are more worker threads than the normal number and they are idle, one worker thread (per reap call) is stopped.

This method is called regularly by the reap timer (see the reap_interval option to the constructor) and it's not typically called by users.

cleanup

Stops all worker threads. Called on editor shutdown.

ACCESSORS

task_queue

Returns the queue of tasks to be processed as a Thread::Queue object. The tasks in the queue have been serialized for passing between threads, so this is mostly useful internally or for checking the number of outstanding jobs.

reap_interval

Returns the number of milliseconds between the regulary cleanup runs.

use_threads

Returns whether running in degraded mode (no threads, false) or normal operation (threads, true).

workers

Returns a list of the worker threads.

EVENT HANDLERS

on_close

Registered to be executed on editor shutdown. Executes the cleanup method.

on_task_done_event

This event handler is called when a background task has finished execution. It deserializes the background task object and calls its finish method with the Padre main window object as first argument. (This is done because finish most likely updates the GUI.)

on_task_start_event

This event handler is called when a background task is about to start execution. It simply increments the running task counter.

TODO

What if the computer can't keep up with the queued jobs? This needs some consideration and probably, the schedule() call needs to block once the queue is "full". However, it's not clear how this can work if the Wx MainLoop isn't reached for processing finish events.

SEE ALSO

The base class of all "work units" is Padre::Task.

AUTHOR

Steffen Mueller smueller@cpan.org

COPYRIGHT AND LICENSE

Copyright 2008-2009 The Padre development team as listed in Padre.pm.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.