Padre::TaskManager - Padre Background Task Scheduler
require Padre::Task::Foo; my $task = Padre::Task::Foo->new(some => 'data'); $task->schedule; # handed off to the task manager
Padre uses threads for asynchronous 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.
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.
Padre::TaskManager
Optional parameters:
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.
Disable for profiling runs. In the degraded, thread-less mode, all tasks are run in the main thread. Default: 1 (use threads)
The number of milliseconds to wait before checking for dead worker threads. Default: 15000ms
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.
Padre::Task
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.
reap_interval
cleanup
Shutdown all services with a HANGUP, then stop all worker threads. Called on editor shutdown.
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.
Returns the number of milliseconds between the regular cleanup runs.
use_threads
Returns whether running in degraded mode (no threads, false) or normal operation (threads, true).
running_tasks
Returns the number of tasks that are currently being executed.
shutdown_services
Gracefully shutdown the services by instructing them to hangup themselves and return via the usual Task mechanism.
workers
Returns a list of the worker threads.
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.)
finish
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.
on_service_poll_event
on_dump_running_tasks
Called by the toolbar task-status button. Dumps the list of running tasks to the output panel.
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.
schedule()
MainLoop
Polling services aliveness in a useful way, something a Wx::Taskmanager might like to display. Ability to selectively kill tasks/services
Wx::Taskmanager
The base class of all "work units" is Padre::Task.
Steffen Mueller smueller@cpan.org
smueller@cpan.org
Copyright 2008-2010 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.
To install Padre, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Padre
CPAN shell
perl -MCPAN -e shell install Padre
For more information on module installation, please visit the detailed CPAN module installation guide.