Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long running tasks #11

Open
matthewp opened this issue Nov 21, 2018 · 1 comment
Open

Long running tasks #11

matthewp opened this issue Nov 21, 2018 · 1 comment

Comments

@matthewp
Copy link

The postTask API is request/response based, and as such doesn't have a natural fit for long running tasks. How do you see such tasks fitting into the model?

For example, let's say I had a worklet which connects to a chat service (via http2 or websocket) and receives chat messages. Currently you could poll the worklet for the next message like so:

const queue = new TaskQueue();

async function run() {
  await queue.addModule('/chat-worklet.js');

  while(true) {
    const task = queue.postTask('nextMessage');
    const message = await task.result;
    addChatMessageToList(message);
  }
}

run();

I guess this is ok but it also doesn't seem ideal to me. I think it would make the worklet side more complex in a lot of cases (you might have to keep some global state to know which messages have been sent to which requester).

It would be nice if tasks could also return a stream or something, so that you could use for/of to await each message.

@developit
Copy link
Owner

Breaking request/response unfortunately makes task chaining impossible. We (will) have Transferable Streams soon, but the use-cases for this are probably better-suited to a typical Dedicated Worker. Thinking about pooling mainly - I can't see a way to pass a stream through a Task without that Task then locking its parent thread (which would mean all results would be transferred between threads).

In general, Task Worklet is suited to parallelizing main-thread-coordinated work, whereas Worker is suited to offloading arbitrary work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants