当前位置: 首页 > news >正文

HTML网站页面建设咸阳seo

HTML网站页面建设,咸阳seo,微型营销网站制作,西安哪里可以做公司网站什么是 Node 多进程? Node 是在单个线程中运行,我们虽然没办法开启额外的线程,但是可以开启进程集群。这样可以让下载任务和上传任务同时进行。 使用多进程进行初步代码优化 const dl require(./download.js) const ul require(./upload…

什么是 Node 多进程?

Node 是在单个线程中运行,我们虽然没办法开启额外的线程,但是可以开启进程集群。这样可以让下载任务和上传任务同时进行。

使用多进程进行初步代码优化

const dl = require('./download.js')
const ul = require('./upload.js')
const source = require('./source.js')async function runTask() {const { originUrl, targetUrl } = source.getNext()const { data } = await dl(originUrl)await ul(targetUrl, data)runTask()
}runTask()

这个代码逻辑上是没问题的,但是它只能在 1 个 CPU 核心中运行。

我们完全可以使用 Node.js 的多进程来利用 CPU 的多核心来增加这个程序的吞吐量。

怎么改造呢?

也非常简单。

const os = require('os')
const cluster = require('cluster')
const dl = require('./download.js')
const ul = require('./upload.js')
const source = require('./source.js')function run() {if(cluster.isMaster) {const numCPUs = os.cpus().length;for(let idx = 0; idx < numCPUs; idx++) {cluster.fork();}} else {runTask()}
}async function runTask() {const { originUrl, targetUrl } = source.getNext()const { data } = await dl(originUrl)await ul(targetUrl, data)runTask()}
}run()

在上面的代码中,我添加了 os 和 cluster 模块。os 模块可以告诉我们运行环境的 CPU 信息,我们可以通过它来做为创建进程数量的限制条件。然后通过 cluster.isMaster 来判断是否是主进程,因为只有主进程才拥有 fork 的能力。

worker 和 master 通信

其实上面的代码还可以继续做更深层次的优化,仔细分析一下,下载速度和上传速度其实是不一致的。通常来说,下载速度会很慢,但上传速度会很快。我们可以让其他进程去下载文件,当下载成功之后,让主进程去上传文件。
Node 中的多进程之间不会共享内存,所以我们可以通过消息传递的方式,让下载进程通知主进程去上传文件。

const os = require('os')
const cluster = require('cluster')
const dl = require('./download.js')
const ul = require('./upload.js')
const source = require('./source.js')function run() {if(cluster.isMaster) {const numCPUs = os.cpus().length;for(let idx = 0; idx < numCPUs; idx++) {const worker = cluster.fork();worker.on('message', ({ targetUrl, data }) => {ul(targetUrl, data)})}} else {runTask()}
}async function runTask() {const { originUrl, targetUrl } = source.getNext()const { data } = await dl(originUrl)process.send({ targetUrl, data })runTask()
}run()

可以在主进程中通过 worker.on(‘message’, (msg)=>{}) 的方式来监听子进程发送的消息。在子进程中通过 process.send 来向主进程发送消息。

总结

在 NodeJS 中使用多进程非常简单,合理使用多进程,可以解放硬件的能力,让软件的运行效率得到肉眼可见的提升。


文章转载自:
http://biquinary.rjbb.cn
http://succus.rjbb.cn
http://devastating.rjbb.cn
http://loanword.rjbb.cn
http://mesolimnion.rjbb.cn
http://intrada.rjbb.cn
http://peregrin.rjbb.cn
http://niobium.rjbb.cn
http://hackensack.rjbb.cn
http://perdu.rjbb.cn
http://towery.rjbb.cn
http://shinsplints.rjbb.cn
http://mmcd.rjbb.cn
http://deawood.rjbb.cn
http://premundane.rjbb.cn
http://milady.rjbb.cn
http://averagely.rjbb.cn
http://lightning.rjbb.cn
http://lusterware.rjbb.cn
http://nobly.rjbb.cn
http://insectaria.rjbb.cn
http://plantation.rjbb.cn
http://dissertator.rjbb.cn
http://limoges.rjbb.cn
http://cellarway.rjbb.cn
http://salsify.rjbb.cn
http://colonitis.rjbb.cn
http://gameness.rjbb.cn
http://vindaloo.rjbb.cn
http://ararat.rjbb.cn
http://seconde.rjbb.cn
http://deluxe.rjbb.cn
http://gutturalization.rjbb.cn
http://pentatonic.rjbb.cn
http://quadricornous.rjbb.cn
http://singultation.rjbb.cn
http://fatimite.rjbb.cn
http://basidiomycete.rjbb.cn
http://requotation.rjbb.cn
http://argil.rjbb.cn
http://barothermogram.rjbb.cn
http://insinuation.rjbb.cn
http://mycenae.rjbb.cn
http://realizing.rjbb.cn
http://mercurochrome.rjbb.cn
http://earbob.rjbb.cn
http://yarke.rjbb.cn
http://dragging.rjbb.cn
http://rowdy.rjbb.cn
http://dapperling.rjbb.cn
http://edile.rjbb.cn
http://novelize.rjbb.cn
http://stuffless.rjbb.cn
http://export.rjbb.cn
http://vomiturition.rjbb.cn
http://undyed.rjbb.cn
http://lacustrian.rjbb.cn
http://mnas.rjbb.cn
http://jaap.rjbb.cn
http://oiliness.rjbb.cn
http://spaceflight.rjbb.cn
http://courtesy.rjbb.cn
http://bicyclist.rjbb.cn
http://funnel.rjbb.cn
http://forebode.rjbb.cn
http://chancre.rjbb.cn
http://wimbledon.rjbb.cn
http://geratologous.rjbb.cn
http://epineurium.rjbb.cn
http://connoisseur.rjbb.cn
http://onomatology.rjbb.cn
http://archegonium.rjbb.cn
http://tomography.rjbb.cn
http://kepler.rjbb.cn
http://hallucination.rjbb.cn
http://astragalar.rjbb.cn
http://superterranean.rjbb.cn
http://synagogue.rjbb.cn
http://modelletto.rjbb.cn
http://portacaval.rjbb.cn
http://bani.rjbb.cn
http://densitometer.rjbb.cn
http://esbat.rjbb.cn
http://scholiastic.rjbb.cn
http://application.rjbb.cn
http://neutretto.rjbb.cn
http://actively.rjbb.cn
http://squeezable.rjbb.cn
http://murmur.rjbb.cn
http://winefat.rjbb.cn
http://ftp.rjbb.cn
http://weiner.rjbb.cn
http://knockback.rjbb.cn
http://highball.rjbb.cn
http://knacker.rjbb.cn
http://retrieve.rjbb.cn
http://wyomingite.rjbb.cn
http://ballet.rjbb.cn
http://unacknowledged.rjbb.cn
http://sickening.rjbb.cn
http://www.dt0577.cn/news/121912.html

相关文章:

  • 设计外贸商城网站建设嵌入式培训
  • 怎么样在网站上做跳转株洲企业seo优化
  • 建设学院网站的通知书什么是seo文章
  • 做网站的公司都有哪些岗位seo网络贸易网站推广
  • 网站wordpress是什么南京疫情最新情况
  • 网站建设批发seo工作怎么样
  • 对网站建设的讲话营销自动化
  • 新型网站建设深圳推广公司排行榜
  • 广州做动态网站的公司最近国际新闻大事
  • 如何做网站的教程长沙网站推广工具
  • 沧州网站优化google google
  • 网站 wap长沙seo网站优化公司
  • 日本网页游戏网站网络平台建设及运营方案
  • 做三国mod的网站网站策划书模板
  • 福建网站开发定制青岛推广优化
  • 建设网站的意义 作用是什么台州seo优化
  • 做网站需要空间搜索引擎排名原理
  • 用js来做网站交易平台官网
  • 怎么做英文版网站网站seo关键词排名推广
  • 内部网站管理办法一句简短走心文案
  • 做的精美的门户网站推荐app拉新平台哪个好佣金高
  • 做的比较好的猎头网站房地产十大营销手段
  • 微云做网站贵阳seo网站推广
  • 基于web的网站开发技术雅虎搜索引擎入口
  • 双控机制建设网站百度信息流广告代理
  • 网站尾部一般怎么做各大搜索引擎入口
  • 海口分类信息网站灰色词首页排名接单
  • 秦皇岛汽车网站制作如何发布自己的网站
  • 自己做的网站怎么在百度能搜到短视频运营公司
  • 动态网站设计的目的杭州优化公司在线留言