Python:同时执行多个功能

use*_*601 20 python

我试图在Python中同时运行两个函数.我尝试了下面的代码,multiprocessing但是当我执行代码时,第二个函数只在第一个函数完成后启动.

from multiprocessing import Process
def func1:
     #does something

def func2:
     #does something

if __name__=='__main__':
     p1 = Process(target = func1)
     p1.start()
     p2 = Process(target = func2)
     p2.start()
Run Code Online (Sandbox Code Playgroud)

Sha*_*ank 46

你正确地做到了.:)

尝试运行这段愚蠢的代码:

from multiprocessing import Process
import sys

rocket = 0

def func1():
    global rocket
    print 'start func1'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func1'

def func2():
    global rocket
    print 'start func2'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func2'

if __name__=='__main__':
    p1 = Process(target = func1)
    p1.start()
    p2 = Process(target = func2)
    p2.start()
Run Code Online (Sandbox Code Playgroud)

您将看到它打印'start func1'然后'start func2',然后在(非常)很长一段时间后,您将看到函数结束.但他们确实会同时执行.

因为进程需要一段时间才能启动,所以你甚至可以 'start func1' 之前看到'start func2 '.


小智 14

这正是我所需要的.我知道它没有被问到但是我修改了shashank的代码以适合Python 3的任何人看:)

from multiprocessing import Process
import sys

rocket = 0

def func1():
    global rocket
    print ('start func1')
    while rocket < sys.maxsize:
        rocket += 1
    print ('end func1')

def func2():
    global rocket
    print ('start func2')
    while rocket < sys.maxsize:
        rocket += 1
    print ('end func2')

if __name__=='__main__':
    p1 = Process(target=func1)
    p1.start()
    p2 = Process(target=func2)
    p2.start()
Run Code Online (Sandbox Code Playgroud)

用sys.maxsize替换一个数字然后打印(火箭),你可以看到它一次计数一个.得到一个数字并停下来

  • 如何使用接受输入参数的函数来完成此操作? (3认同)
  • 知道为什么这根本不会做任何事情吗? (2认同)

Ion*_*ica 6

这可以通过Ray优雅地完成,这是一个允许您轻松并行化和分发 Python 代码的系统。

要并行化您的示例,您需要使用 定义函数@ray.remote decorator,然后使用 调用它们.remote

import ray

ray.init()

# Define functions you want to execute in parallel using 
# the ray.remote decorator.
@ray.remote
def func1():
    #does something

@ray.remote
def func2():
    #does something

# Execute func1 and func2 in parallel.
ray.get([func1.remote(), func2.remote()])
Run Code Online (Sandbox Code Playgroud)

如果func1()func2()返回结果,则需要将代码改写如下:

ret_id1 = func1.remote()
ret_id2 = func1.remote()
ret1, ret2 = ray.get([ret_id1, ret_id2])
Run Code Online (Sandbox Code Playgroud)

多处理模块相比,使用 Ray 有许多优点。特别是,相同的代码将在单台机器和机器集群上运行。有关 Ray 的更多优势,请参阅此相关帖子

  • 对我来说不幸的是,窗口没有光线分布。 (2认同)

The*_*ata 5

这是@Shashank 的一个很好的例子。我只想说join最后必须加上,不然两个进程不是同时运行的:

from multiprocessing import Process
import sys

rocket = 0

def func1():
    global rocket
    print 'start func1'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func1'

def func2():
    global rocket
    print 'start func2'
    while rocket < sys.maxint:
        rocket += 1
    print 'end func2'

if __name__=='__main__':
    p1 = Process(target = func1)
    p1.start()
    p2 = Process(target = func2)
    p2.start()
    # This is where I had to add the join() function.
    p1.join()
    p2.join()
Run Code Online (Sandbox Code Playgroud)

此外,请查看此线程: 何时在进程上调用 .join()?