我刚刚开始使用Python使用Apache Beam。我有一个希望并行运行的任务,但由于某种原因,它可以串行运行。有什么我完全误会的东西吗?
在下面的示例中,我希望RunInParallel
同时执行。 DoFn
需要2秒钟的处理时间,因此我希望下面的管道在2秒钟内完成。但是,这需要5秒钟2秒。
import apache_beam as beam
import time
from apache_beam.options.pipeline_options import PipelineOptions
class RunInParallel(beam.DoFn):
def process(self, element, *args, **kwargs):
time.sleep(2)
return [int(time.time())]
p = beam.Pipeline(options=PipelineOptions())
(p
| 'Create tasks' >> beam.Create([i for i in range(5)])
| "Run task" >> beam.ParDo(RunInParallel())
| "Print timestamp" >> beam.Map(print)
)
# Run the pipeline
res = p.run()
上述管道的输出为
1573065709
1573065711
1573065713
1573065715
1573065717