Python多处理输出

Python多处理输出,python,python-multiprocessing,Python,Python Multiprocessing,我有一个进度条功能。我想在另一个函数进行处理时运行此进度条。我使用多处理模块编写了一个简单的测试代码,但效果不好。我的代码: from multiprocessing import Process import time def foo(thread): print time.ctime() time.sleep(10) print time.ctime() def progress_bar(timer = 10): digits = 4 delete

我有一个进度条功能。我想在另一个函数进行处理时运行此进度条。我使用多处理模块编写了一个简单的测试代码,但效果不好。我的代码:

from multiprocessing import Process
import time

def foo(thread):
    print time.ctime()
    time.sleep(10)
    print time.ctime()

def progress_bar(timer = 10):
    digits = 4
    delete = '\b' * 6
    time_slot = float(timer) / 100
    for i in range(1, 101):
        delete_bar = '\b' * 52
        if i == 1:
            bar = '|' + ' ' * 50 + '|'
        else:
            bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
        print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
        time.sleep(time_slot)
    print ''

def main():
    p1 = Process(target = foo1('this'))
    p1.start()
    p2 = Process(target = progress_bar())
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    main()
我希望foo会先打印当前时间。然后进度条进入倒计时10秒。最后,foo将在最后输出另一个时间

Tue Apr  5 11:49:47 2016
100% =================================================>|
Tue Apr  5 11:49:57 2016
然而,我从输出中得到的结果如下:

Tue Apr  5 11:49:47 2016
Tue Apr  5 11:49:57 2016
100% =================================================>|
有没有办法用Python解决这个问题?
多谢各位

发生这种情况是因为首先计算函数参数,在定义
p1
p2
时,实际上调用的是
foo1('This')
,它在
p1
的定义处执行函数,在
p2
的实例化处执行
progress\u bar()

有关演示这一点的简单示例,请参见以下内容:

def fn():
    print 'called'
    return 1

target1 = fn()
target = fn

print target1
print target
这张照片是:

>>> called # Got called as soon as you called fn via fn()
>>> 1 # Assigned the return value of fn to target1
>>> <function fn at 0x12DA77F0> # Didn't get called, assigned the fn definition to target
这张照片

Tue Apr 05 15:09:34 2016
('1%  |                                                  |',)
('2%  |>                                                 |',)
('3%  |>                                                 |',)
('4%  |=>                                                |',)
('5%  |=>                                                |',)
('6%  |==>                                               |',)
('7%  |==>                                               |',)
('8%  |===>                                              |',)
('9%  |===>                                              |',)
('10% |====>                                             |',)
('11% |====>                                             |',)
('12% |=====>                                            |',)
('13% |=====>                                            |',)
('14% |======>                                           |',)
('15% |======>                                           |',)
('16% |=======>                                          |',)
('17% |=======>                                          |',)
('18% |========>                                         |',)
('19% |========>                                         |',)
('20% |=========>                                        |',)
('21% |=========>                                        |',)
('22% |==========>                                       |',)
('23% |==========>                                       |',)
('24% |===========>                                      |',)
('25% |===========>                                      |',)
('26% |============>                                     |',)
('27% |============>                                     |',)
('28% |=============>                                    |',)
('29% |=============>                                    |',)
('30% |==============>                                   |',)
('31% |==============>                                   |',)
('32% |===============>                                  |',)
('33% |===============>                                  |',)
('34% |================>                                 |',)
('35% |================>                                 |',)
('36% |=================>                                |',)
('37% |=================>                                |',)
('38% |==================>                               |',)
('39% |==================>                               |',)
('40% |===================>                              |',)
('41% |===================>                              |',)
('42% |====================>                             |',)
('43% |====================>                             |',)
('44% |=====================>                            |',)
('45% |=====================>                            |',)
('46% |======================>                           |',)
('47% |======================>                           |',)
('48% |=======================>                          |',)
('49% |=======================>                          |',)
('50% |========================>                         |',)
('51% |========================>                         |',)
('52% |=========================>                        |',)
('53% |=========================>                        |',)
('54% |==========================>                       |',)
('55% |==========================>                       |',)
('56% |===========================>                      |',)
('57% |===========================>                      |',)
('58% |============================>                     |',)
('59% |============================>                     |',)
('60% |=============================>                    |',)
('61% |=============================>                    |',)
('62% |==============================>                   |',)
('63% |==============================>                   |',)
('64% |===============================>                  |',)
('65% |===============================>                  |',)
('66% |================================>                 |',)
('67% |================================>                 |',)
('68% |=================================>                |',)
('69% |=================================>                |',)
('70% |==================================>               |',)
('71% |==================================>               |',)
('72% |===================================>              |',)
('73% |===================================>              |',)
('74% |====================================>             |',)
('75% |====================================>             |',)
('76% |=====================================>            |',)
('77% |=====================================>            |',)
('78% |======================================>           |',)
('79% |======================================>           |',)
('80% |=======================================>          |',)
('81% |=======================================>          |',)
('82% |========================================>         |',)
('83% |========================================>         |',)
('84% |=========================================>        |',)
('85% |=========================================>        |',)
('86% |==========================================>       |',)
('87% |==========================================>       |',)
('88% |===========================================>      |',)
('89% |===========================================>      |',)
('90% |============================================>     |',)
('91% |============================================>     |',)
('92% |=============================================>    |',)
('93% |=============================================>    |',)
('94% |==============================================>   |',)
('95% |==============================================>   |',)
('96% |===============================================>  |',)
('97% |===============================================>  |',)
('98% |================================================> |',)
Tue Apr 05 15:09:44 2016
('99% |================================================> |',)
('100%|=================================================>|',)
试试这个:

from multiprocessing import Process
import time

def foo1(thread):
    print time.ctime()
    time.sleep(10.5)
    print time.ctime()

def progress_bar(timer = 10):
    digits = 4
    delete = '\b' * 6
    time_slot = float(timer) / 100
    for i in range(1, 101):
        delete_bar = '\b' * 52
        if i == 1:
            bar = '|' + ' ' * 50 + '|'
        else:
            bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
        print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
        time.sleep(time_slot)
    print ''

def main():
    p1 = Process(target=foo1, args=('this',))
    # p1 = Process(target = foo1('this'))
    p1.start()
    time.sleep(0.1)
    p2 = Process(target=progress_bar)
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    main()

请注意不同的p1进程。

尝试重新运行
进程
示例再次使我的计算机崩溃--问题似乎来自进程(target=foo(1))调用线路。我能够用Process(target=foo,args=('this',)输出正确的结果。非常感谢你!事实上,这还不是全部。在实例化第二个流程时,也需要去掉括号。有了它们,您的代码就相当于只说
p1.start()
,然后说
progress\u bar()
,它只在主进程上运行,使得第二个进程毫无意义。您可以尝试添加第三个过程来了解我的意思。@yc2986请查看答案中添加的另一个基本示例。这就是我想要的!非常感谢你!
from multiprocessing import Process
import time

def foo1(thread):
    print time.ctime()
    time.sleep(10.5)
    print time.ctime()

def progress_bar(timer = 10):
    digits = 4
    delete = '\b' * 6
    time_slot = float(timer) / 100
    for i in range(1, 101):
        delete_bar = '\b' * 52
        if i == 1:
            bar = '|' + ' ' * 50 + '|'
        else:
            bar = '|' + '=' * (i / 2 - 1) + '>' + ' ' * (50 - i / 2) + '|'
        print "{0}{1:{2}}{3}{4}".format(delete, str(i) + '%', digits, bar, delete_bar),
        time.sleep(time_slot)
    print ''

def main():
    p1 = Process(target=foo1, args=('this',))
    # p1 = Process(target = foo1('this'))
    p1.start()
    time.sleep(0.1)
    p2 = Process(target=progress_bar)
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    main()