是否可以在不将助焊剂转化为流的情况下从助焊剂中获得第一个元素?

时间:2019-03-05 11:02:28

标签: java spring-webflux project-reactor reactive

是否有可能从通量中获得第一个元素而不将通量转换为流?

2 个答案:

答案 0 :(得分:2)

Flux.range(1,10).take(1)应该可以解决问题(此处的range(...)部分只是发出一些样本值;如果您对流进行blockFirst()subscribe(),则应该看到< strong>“ 1” )

答案 1 :(得分:1)

除了take(1)之外,如果您需要代表Mono<T>的第一个元素的Flux<T>,则可以使用.next()

或者,如果您需要第i个元素,请使用.elementAt(i)(但必须确保存在这样的元素,与takenext不同的是,import multiprocessing print(f'num cpus {multiprocessing.cpu_count():d}') import sys; print(f'Python {sys.version} on {sys.platform}') def _process(m): print(m) #; return m raise ValueError(m) args_list = [[i] for i in range(1, 20)] if __name__ == '__main__': with multiprocessing.Pool(2) as p: print([r for r in p.starmap(_process, args_list)]) num cpus 8 Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) [Clang 6.0 (clang-600.0.57)] on darwin 1 7 4 10 13 16 19 multiprocessing.pool.RemoteTraceback: """ Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 121, in worker result = (True, func(*args, **kwds)) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 47, in starmapstar return list(itertools.starmap(args[0], args[1])) File "/Users/ubik-mac13/Library/Preferences/PyCharm2018.3/scratches/multiprocess_error.py", line 8, in _process raise ValueError(m) ValueError: 1 """ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/ubik-mac13/Library/Preferences/PyCharm2018.3/scratches/multiprocess_error.py", line 18, in <module> print([r for r in p.starmap(_process, args_list)]) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 298, in starmap return self._map_async(func, iterable, starmapstar, chunksize).get() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 683, in get raise self._value ValueError: 1 Process finished with exit code 1 仅返回一个空的发布者足够的元素)。