Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法同时运行两个循环(for循环,while循环…)?_Python - Fatal编程技术网

Python 有没有办法同时运行两个循环(for循环,while循环…)?

Python 有没有办法同时运行两个循环(for循环,while循环…)?,python,Python,有没有办法运行两个循环的循环,而循环,。。。同时在皮顿?举个简单的例子,我想同时并行运行两个For循环,而不是一个接一个地线性运行。如果可能,请向我展示此脚本中的循环: for x in range (0,10): print('hello world') for x in range(0,10 ): print ('welcome to the world') 最简单的方法是使用numba: @jit(nopython=True, parallel=True) def sim

有没有办法运行两个循环的循环,而循环,。。。同时在皮顿?举个简单的例子,我想同时并行运行两个For循环,而不是一个接一个地线性运行。如果可能,请向我展示此脚本中的循环:

for x in range (0,10):
    print('hello world')
for x in range(0,10 ):
    print ('welcome to the world')

最简单的方法是使用numba:

@jit(nopython=True, parallel=True)
def simulator(out):
    # iterate loop in parallel
    for i in prange(out.shape[0]):
        print('hello world')
        print('welcome to the world')

另一种方法是使用asyncio,但处理起来更复杂。

这似乎是一个常见的话题,你做过研究吗?这是一个纯粹的概念性问题。如果你真的想做并行处理,你必须创建进程或线程。如果要在同步循环中执行2次思考,请只创建一个循环,并在其中执行这两个任务。