question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TaskManager: Proposed worker management class

See original GitHub issue

Following up on a conversation in https://github.com/mrdoob/three.js/pull/18123, this is a proposal for a utility class for managing tasks distributed to Web Workers. Currently BasisTextureLoader, DRACOLoader, and OBJLoader2 implement redundant logic for that purpose.

A full-featured library for this purpose could easily get pretty complex. I’m hoping that we can write something fairly lightweight for internal use by threejs examples. A more robust version could probably be a standalone library, or part of ECSY, but that’s beyond the scope I personally want to attempt.

Proposed API:

interface TaskManager {

  /** Returns true if support for the given task type is available. */
  supportsType( type: string ): boolean;

  /** Registers functionality for a new task type. */
  registerType( type: string, init: Function, execute: Function ): TaskManager;

  /** Provides initialization configuration and dependencies for all tasks of given type. */
  initType( type: string, config: object, transfer: Transferrable[] ): TaskManager;

  /** Queues a new task of the given type. Task will not execute until initialization completes. */
  addTask( type: string, cost: number, config: object, transfer: Transferrable[] ): Promise<any>;

  /** Destroys all workers and associated resources. */
  dispose(): TaskManager;

}

Use in DRACOLoader:

const DRACO_DECODE = 'draco/decode';

class DRACOLoader {

  constructor ( loadingManager: LoadingManager, taskManager: TaskManager ) {

    this.loadingManager = loadingManager || DefaultLoadingManager;
    this.taskManager = taskManager || DefaultTaskManager;

    if ( ! this.taskManager.supportsType( DRACO_DECODE ) ) {

      this.taskManager
        .registerType( DRACO_DECODE, taskInit, taskExecute )
        .initType( DRACO_DECODE, decoderConfig, decoderTransfers );

    }

  }

  load ( url, onLoad, onProgress, onError ) {

    const data = await fetch( url ).then( r => r.arrayBuffer() );
    const cost = data.byteLength;
    const config = { data, ... };

    this.taskManager
      .addTask( DRACO_DECODE, cost, config, [ data ] )
      .then( onLoad ).catch( onError );

  }

}

These two functions are passed to the TaskManager, and their function bodies are copied into the Web Worker (without surrounding context).

// Sets up state before a worker begins taking Draco tasks.
function taskInit ( config: object ): Promise<void> {

  // do async setup

  return Promise.resolve();

}

// Executes on a worker for each Draco task.
function taskExecute ( taskConfig: object ): Promise<any> {

  // do expensive processing

  return Promise.resolve( result );

} 

The TaskManager class then takes on the responsibilities of:

  • Creating a configurable number of Web Workers.
  • Installing the dependencies (i.e. function bodies + transfers) for each supported task type.
  • Distributing tasks evenly across workers.
  • Disposing of workers if necessary.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:26 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
donmccurdycommented, May 25, 2022

Between @kaisalmen’s work in https://github.com/kaisalmen/wtd, and the small WorkerPool.js utility in the three.js repository, I believe this issue can be resolved. 🎉

1reaction
kaisalmencommented, Apr 18, 2020

Am able to make all eight logical cores 100% busy: One simple worker (10^8 additions in for loop), 8 instance, 1000 added tasks . I have not modified any loader code, because I wanted to get the concept straight, first. This is still WIP. Sorry, things move very slow, but spare time is more rare than usual for me these days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Best Task Manager Certifications in 2022 - Zippia
1. Project Management Professional (PMP) · More than two years of education or training after high school required? No · More than two...
Read more >
Notion Masterclass: Build a Task Manager from Scratch
In this in-depth guide, you'll learn how to create a full-featured task and project manager inside of Notion that features a project area, ......
Read more >
Register a task - Sitecore Documentation
Use the task manager ( TaskManager ) to register a distributed task, a deferred ... Uses the DeferredWorkerOptionsDictionary base class:.
Read more >
23 Best Task Management Software for Work in 2022 (Free ...
Here are the 23 best task management software available today. Use these tools for managing simple tasks or more complex projects.
Read more >
Task Manager (Windows) - Wikipedia
Task Manager, previously known as Windows Task Manager, is a task manager, system monitor, and startup manager included with Microsoft Windows systems.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found