NPM 运行并行任务,但要等到资源可用才能运行第二个任务 [英] NPM run parallel task, but wait until resource is available to run second task

查看:59
本文介绍了NPM 运行并行任务,但要等到资源可用才能运行第二个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 npm 中,如何运行两个或多个并行任务,但要等待第一个任务将创建的资源可供第二个任务使用,等等?

In npm, how can I run two or more parallel tasks, but waiting for the resource that the first task will create to be available to the second task to use it, and so forth?

示例(概念):

npm run task1 & waitfor task1 then task2 & waitFor task3 then task4 ...

有什么想法吗?

编辑

举个例子:假设我的第一个任务是启动一个网络服务器,而我的第二个任务是在每次事件发生时从该网络服务器消耗数据.另一个例子:我的第一个任务可能是启动 webdriver-manager,我的第二个任务是启动一个网络服务器,我的第三个任务是每次我的文件被更改时运行 e2e 测试.所以,我需要所有这些任务保持并发运行,但它们需要按照特定的顺序和时间进行初始化.

As an example: Lets say that my first task is starting a webserver, and my second task is consuming data from that web-server every time an event happens. Another example: My first task could be starting webdriver-manager, my second task, starting a webserver, and my third task, run e2e tests everty time my files are changed. So, I need all those tasks to keep running concurrently, but they need to be initialized in an specific order and time.

推荐答案

您可以尝试同时与<一个 href="https://github.com/jeffbski/wait-on" rel="noreferrer">wait-on 包,用于管理并发/顺序脚本和输出.wait-on 顺序启动支持头部响应状态,tcp 监听,...

You can try concurrently with wait-on package in order to manage conccurent/sequential scripts and outputs. wait-on sequential launches support head response status, tcp listening, ...

例如:

"scripts": {
    "start": "concurrently -k -n \"DB,API,WEB\" -c \"blue,magenta,yellow\" \"npm run start-db\" \"npm run start-api\" \"npm run start-web\"",
    "start-db": "myDbServerCmd",
    "start-api": "wait-on tcp:27017 && myApiServerCmd",
    "start-web": "myFrontServerCmd",
}

感谢 dchambers 的想法(来源).

Thanks dchambers for the idea (source).

这篇关于NPM 运行并行任务,但要等到资源可用才能运行第二个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆