javascript - 是否可以使用 workerpool 从网站获取?
问题描述
我正在尝试创建一个工作池,其中一个函数获取网站上的某些数据。但我似乎无法从获取中得到任何东西。
还有另一种获取方式吗?还是我做错了什么?
这是我的代码:
运行脚本.js
const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/myWorker.js');
pool.proxy()
.then(function (worker) {
worker.asyncFetch().then(r => console.log(r));
})
.catch(function (err) {
console.error(err);
})
.then(function () {
pool.terminate(); // terminate all workers when done
});
myWorker.js
const workerpool = require('workerpool');
function asyncFetch () {
const fetch = require('node-fetch');
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(data => {
console.log('the data');
console.log(data)
});
return fetch('https://jsonplaceholder.typicode.com/todos/1');
}
// an async function returning a promise
function asyncMultiply (a, b) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(a * b);
}, 1000);
});
}
// create a worker and register public functions
workerpool.worker({
asyncFetch: asyncFetch,
asyncMultiply: asyncMultiply
});
解决方案
推荐阅读
- docker - 如何使用 docker-compose 在分布式气流架构上配置 celery worker?
- sql - 使用基于类的视图的 Django 原始 sql 查询问题
- ansible - Ansible如何按键删除组值
- python - 检查文本字符串是否包含文本或类似文本
- python - matplotlib 使用条形图显示浮动值的意外行为
- modelica - 如何使 Dymola 中的动态模型与稳态设计结果一致?
- oracle - 如何以非管理员身份运行 dbms_output 的功能?
- apache-kafka-streams - 双重警告记录来自 DeserializationExceptionHandler 和 RecordDeserializer 的反序列化失败
- spring - 数据源问题:java.lang.ClassNotFoundException:org.springframework.jdbc.datasource.DriverManagerDataSource
- python - 通过连接从元组的多个元素创建火炬张量的简单方法