-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (42 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
import { Worker, isMainThread } from "node:worker_threads"
import path, { dirname } from "node:path"
import { fileURLToPath } from "node:url"
import Piscina from "piscina"
const __filename = fileURLToPath(import.meta.url)
// use the filename pass from arguments
let file = process.argv[2]
// get
// this is the main that can spawn other worker threads;
console.log(file)
// create a threadpool
let pool = new Piscina()
const options = { filename: "./threadpoolWorker.js" }
let sizes = [1080, 720, 480]
/*
const worker = new Worker("./worker.js", {
workerData: {
filename: file,
dir: path.join("./compressed"),
size: sizes[i],
},
})
// handle messages from worker_threads
worker.on("message", (msg) => {
str += `,${msg}`
})
worker.on("error", (err) => {
throw err
})
*/
console.time("threadpool")
let result = await Promise.all(
sizes.map((size) =>
pool.run(
{ filename: file, size, dir: "compressed", hwaccel: true },
options
)
)
)
console.log(result)
console.timeEnd("threadpool")