duart38 / thread Goto Github PK
View Code? Open in Web Editor NEWtype safe multi-threading made easier
License: MIT License
type safe multi-threading made easier
License: MIT License
I have simple folder structure:
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?
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)
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
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...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.