Coder Social home page Coder Social logo

duart38 / thread Goto Github PK

View Code? Open in Web Editor NEW
45.0 45.0 3.0 114 KB

type safe multi-threading made easier

License: MIT License

TypeScript 94.99% JavaScript 5.01%
deno module multithreading thread threading typesafe typescript webworker webworkers worker

thread's Introduction

thread's People

Contributors

duart38 avatar grevend avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

thread's Issues

Unable to import local files in threads

consider this:

import Thread from "https://deno.land/x/[email protected]/Thread.ts";
import { Cat } from "./cat.ts";

const nWorkers = Math.max(Math.floor(navigator.hardwareConcurrency * 0.8), 2);


const threads: Array<Thread> = [];
for (let i = 0; i < nWorkers; ++i) {
  threads.push(
    new Thread<number | undefined, string>(
      (e) => {
        if (e.data.length === 0 || !e.data.length) return;
        const serializer = new Serializer({ classes: { Cat } });
        console.info(e.data);
        const cat: Cat = serializer.deserialize(e.data);
        cat.meow();
        return 0;
      },
      "module",
      [
        'import { Cat } from "./cat.ts"',
        'import { Serializer } from "https://deno.land/x/superserial/mod.ts"',
      ],
    ),
  );
}

let i = 0;
for (const thread of threads) {
  const cat = new Cat(`Matt${i}`);
  thread.onMessage((e) => {
    console.info(`thread -> ${i} : ${e}`);
    ++i;
    thread.stop();
  }).postMessage(serializer.serialize(cat));
}

It tries to import { Cat } from Download https://deno.land/x/[email protected]/cat.ts (1/2)???
I have no idea why it tries fetching it from there. Tried to debug it but i have no idea at which point it appends this library to it...

[RFC] Support async handler on thread function

I have proposal to make async handler works inside thread, Here is sample of async handler

import Thread from "../Thread.ts";

let thread = new Thread<number>(async (e: MessageEvent) => {
  console.log("Worker: Message received from main script");
  const result = e.data[0] * e.data[1];
  await new Promise((resolve) => setTimeout(resolve, 5 * 1000))
  if (isNaN(result)) {
    return 0;
  } else {
    console.log("Worker: Posting message back to main script");
    return (result);
  }
}, "module");

thread.onMessage((e) => {
  console.log(`recived back from thread: ${e}`);
});
thread.postMessage([10, 12]);
thread.postMessage([10, 10]);

//thread.stop();

It handy to have async handler so I can use await inside those function. I can prepare a PR if you want to implement this

Paths not correct when importing from net

error: Uncaught Error: Import 'https://raw.githubusercontent.com/duart38/Thread/master/tmp_threads/deno_thread_4bcab5c4.js' failed: 404 Not Found
    at Worker.#poll (rt/11_workers.js:178:19)

Importing local bundle tries to import from path prefixed with denoland URL

I have simple folder structure:

  • deps.js - bundle which exports Abort
  • main.ts - file where I create worker and try to import Abort from deps.js
new Thread(fetcher, "module", ['import { Abort } from "./deps.js";'])
    .postMessage(config)
    .onMessage(handleResults);

Error I'm getting when running deno run --allow-read --allow-net main.ts

Download https://deno.land/x/[email protected]/deps.js
error: Uncaught (in promise) TypeError: Module not found "https://deno.land/x/[email protected]/deps.js".
      let x = await import(fqfn); //Deno.realPathSync(fqfn)
              ^
    at async Thread.copyDep (https://deno.land/x/[email protected]/Thread.ts:85:15)
    at async https://deno.land/x/[email protected]/Thread.ts:42:58
    at async Promise.all (index 0)
    at async Thread.populateFile (https://deno.land/x/[email protected]/Thread.ts:44:8)

I was trying to find why it's trying to import from https://deno.land/x/[email protected]/deps.js but no clue. It seams to be happening here but I don't know how to debug Deno import.

I basically just followed this example.

Is this possible or am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.