Gulp任务不是串行运行的

时间:2014-12-16 18:32:41

标签: javascript node.js gulp

我遵循官方文档,但我的gulp任务不是连续运行。

gulp.task("mytask", ["foo", "bar", "baz"]);

gulp.task("foo", function (callback) {
  gulp
    .src("...")
    .pipe(changed("..."))
    .pipe(gulp.dest(function (file) {
      // ...stuff
      return "...";
    }))
    .on("end", function() {
      // ...stuff
      callback();
    });
});

gulp.task("bar", function (callback) {
  //...
});

gulp.task("baz", function (callback) {
  //...
});

但我的输出看起来像这样:

Starting 'mytask'...
Starting 'foo'...
Starting 'bar'...                   // <-- foo is not done yet!
Finished 'foo'
Finished 'bar'
Starting 'baz'...
Finished 'baz'
Finished 'mytask'

如何让它们按顺序运行?

2 个答案:

答案 0 :(得分:7)

如果您希望它们按顺序运行,您当前必须使用任务依赖系统,例如:

gulp.task("mytask", ["foo", "bar", "baz"]);

gulp.task("foo", function (callback) {
  //...
  callback(...);
});

gulp.task("bar", ['foo'], function (callback) {
  //...
  callback(...);
});

gulp.task("baz", ['bar'], function (callback) {
  //...
  callback(...);
});

很笨重。我认为它将在未来版本中得到解决。

根据情况,你可以return a promise or event stream而不是传入并呼叫回叫。

我想我应该提到run-sequence模块现在是一个选项。但是上面说明的任务依赖系统是gulp本身目前提供的机制。请参阅this comment re:run-sequence以及gulp中任务排序的未来。

答案 1 :(得分:1)

此答案应进行更新,以反映Gulp 4系列任务的运行方式。

如果您希望Gulp任务按序列运行,则应使用gulp.series来运行它们,如果希望它们并行运行gulp.parallel。

在使用Gulp系列产品时,您将执行以下操作:

# In[11]:

# Import libraries:

import pandas as pd
import numpy as np
from numpy.random import gamma # gamma function
import seaborn as sns # plotting library

# plot histograms immediately:
get_ipython().run_line_magic('matplotlib', 'inline')


# In[12]:


# Define functions

def get_samples_from_gamma_dist( num_of_samples, size_of_samples, alpha, lamb ):
    '''
    Returns table with ( num_of_samples ) rows and ( size_of_samples ) columns.
    Cells in the table are i.i.d sample values from numpy's gamma function
    with shape parameter ( alpha ) and scale parameter ( lamb ).
    '''
    return pd.DataFrame( 
            data = gamma( 
                    shape = alpha, 
                    scale = lamb, 
                    size = 
                        ( 
                            num_of_samples, 
                            size_of_samples 
                        )
                )
            )

# Returns alpha_hat of a sample:
get_alpha_hat = lambda sample : ( sample.mean()**2 ) / sample.var()

# Returns lambda_hat of a sample:
get_lambda_hat = lambda sample : sample.mean() / sample.var()


# In[13]:


# Retrieve samples

# Declaring variables...
my_num_of_samples = 1000
my_size_of_samples = 227
my_alpha = 0.375
my_lambda = 1.674

# Initializing table...
data = get_samples_from_gamma_dist( 
    num_of_samples= my_num_of_samples, 
    size_of_samples= my_size_of_samples, 
    alpha= my_alpha, 
    lamb= my_lambda 
)

# Getting estimated parameter values from each sample...
alpha_hats = data.apply( get_alpha_hat, axis = 1 ) # apply function across the table's columns
lambda_hats = data.apply( get_lambda_hat, axis = 1 ) # apply function across the table's columns


# In[14]:


# Plot histograms:

# Setting background of histograms to 'whitegrid'...
sns.set_style( style = 'whitegrid' )

# Plotting the sample distribution of alpha_hat...
sns.distplot( alpha_hats, 
             hist = True, 
             kde = True, 
             bins = 50, 
             axlabel = 'Estimates of Alpha',
             hist_kws=dict(edgecolor="k", linewidth=2),
             color = 'red' )


# In[15]:


# Plotting the sample distribution of lambda_hat...
sns.distplot( lambda_hats, 
             hist = True, 
             kde = True, 
             bins = 50, 
             axlabel = 'Estimates of Lambda',
             hist_kws=dict(edgecolor="k", linewidth=2),
             color = 'purple' )


# In[16]:


# Print results:

print( "Mean of alpha_hats =", alpha_hats.mean(), '\n'  )

print( "Mean of lambda_hats =", lambda_hats.mean(), '\n' ) # about 0.62

print( "Standard Error of alpha_hats =", alpha_hats.std( ddof = 0 ), '\n'  )

print( "Standard Error of lambda_hats =", lambda_hats.std( ddof = 0 ), '\n'  )

其他任务可能不再是任务,而是const,例如

gulp.task("mytask", gulp.series(foo, bar, baz));

因此,该系列列出了常量而不是一些字符串的原因。迁移到Gulp 4可能还会出现其他问题,但是像这样的简单gulp文件的修复很容易实现。

关于gulp 4 https://codeburst.io/switching-to-gulp-4-0-271ae63530c0的简单教程