Python中的多处理:在同一时间执行两个函数

时间:2012-11-20 14:14:46

标签: python parallel-processing multiprocessing

我想在同一时间执行以下两个功能。

from multiprocessing import Process
import os
import datetime

def func_1(title):
    now = datetime.datetime.now()
    print "hello, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond

def func_2(name):
    func_1('function func_2')
    now = datetime.datetime.now()
    print "Bye, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond

if __name__ == '__main__':
    p = Process(target=func_2, args=('bob',))
    p.start()
    p.join()

我在微秒上有所不同。有没有办法在同一时间执行这两个?任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:5)

这通常是不可能的(“精确”部分)和(2)不是Python擅长的东西。如果你真的需要微秒执行精度,使用C或ASM,但是比COpython's answer更接近的方式是忙于等待两个不同的进程达成一致的开始时间:

from multiprocessing import Process
import os
import datetime
from time import time

def func_1(title):
    now = datetime.datetime.now()
    print "hello, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond

def func_2(name):
    now = datetime.datetime.now()
    print "Bye, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond

def start_f1(name):
    while time() < start_time: pass
    func_1(name)

def start_f2(name):
    while time() < start_time: pass
    func_2(name)        

if __name__ == '__main__':
    procs = []
    procs.append(Process(target=start_f2, args=('bob',)))
    procs.append(Process(target=start_f1, args=('sir',)))
    start_time = time() + 10
    map(lambda x: x.start(), procs)
    map(lambda x: x.join(), procs)

答案 1 :(得分:5)

在计算机上写入了以下内容,此代码始终打印出相同的时间戳:

#! /usr/bin/env python3
from multiprocessing import Barrier, Lock, Process
from time import time
from datetime import datetime

def main():
    synchronizer = Barrier(2)
    serializer = Lock()
    Process(target=test, args=(synchronizer, serializer)).start()
    Process(target=test, args=(synchronizer, serializer)).start()

def test(synchronizer, serializer):
    synchronizer.wait()
    now = time()
    with serializer:
        print(datetime.fromtimestamp(now))

if __name__ == '__main__':
    main()

答案 2 :(得分:2)

我不确定这是否会在完全同时执行,但我认为它会让你更接近。

from multiprocessing import Process
import os
import datetime


def func_1(title):
    now = datetime.datetime.now()
    print "hello, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond


def func_2(name):
    now = datetime.datetime.now()
    print "Bye, world"
    print "Current second: %d" % now.second
    print "Current microsecond: %d" % now.microsecond


if __name__ == '__main__':
    procs = []
    procs.append(Process(target=func_2, args=('bob',)))
    procs.append(Process(target=func_1, args=('sir',)))
    map(lambda x: x.start(), procs)
    map(lambda x: x.join(), procs)

答案 3 :(得分:1)

CPython本质上是单线程的(Google“Global Interpreter Lock”)。为了获得理论上的机会,你需要一个多核处理器,但即使这样,只有一个操作系统能够以非常低的水平运行,即使这样你也需要特殊的硬件。你要的是,在任何实际意义上,不可能。