2016-04-05 16 views
1

私はプログレスバー機能を持っています。他の機能が処理をしている間、このプログレスバーを実行したいと思います。私はマルチプロセッシングモジュールを使って簡単なテストコードを書いていますが、うまく機能しませんでした。私のコード:Pythonマルチプロセッシング出力

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が現在の時刻を最初に表示することを望んでいました。その後progress_barが10秒間カウントダウンします。最後に、fooは最後に別の時刻を出力します。

Tue Apr 5 11:49:47 2016 
Tue Apr 5 11:49:57 2016 
100% =================================================>| 

Pythonでこれを修正する方法はあります:

Tue Apr 5 11:49:47 2016 
100% =================================================>| 
Tue Apr 5 11:49:57 2016 

は、しかし、このようなもの、私は出力からもらったのですか? ありがとうございます!関数のパラメータが最初に評価されるため、そしてあなたのp1を定義するときとp2あなたが実際にp2のインスタンス化でp1、およびprogress_bar()の定義で機能を実行するfoo1('this')を呼んでいる

答えて

1

はこれを試してみてください印刷します

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プロセス。

+1

これは私が欲しいものです!どうもありがとうございました! – yc2986

+0

あなたは大歓迎です! – RiTu

1

これが起こっています。これを示して簡単な例

、以下を参照:

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 

私は以下Thread S(編集での作業あなたの例を得た:いくつかを検討していた後、 Processexamplesのように、以下のコードと同じ構文で作業する必要があります(ちょうどインポートを変更し、の代わりにProcessを使用してください)。)、何らかの理由で、たとえ例をコピーしても、Processの印刷方法を取得できません。私のカスタムPythonのセットアップのために、しかし全くわからないかもしれません):。

from threading import Thread 
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(): 
    t1 = Thread(target=foo, args=('this',)) # Notice, not foo('this') <- this executes foo('this') at definition 
    t1.start() 
    t2 = Thread(target=progress_bar) # Again, notice, no parens - target is just the function definition 
    t2.start() 
    t1.join() 
    t2.join() 

これは

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%|=================================================>|',) 
+0

プロセスの例を再度実行しようとすると、コンピュータがクラッシュしました.- – Bahrom

+0

問題はプロセス(target = foo(1))の呼び出し行から発生しているようです。私はProcess(target = foo、args =( 'this'、))で正しい結果を出力できます。どうもありがとうございました! – yc2986

+0

それは実際にはまだすべてではありません。 ***また、2番目のプロセスもインスタンス化するときにかっこを取り除く必要があります。***これらのコードでは、あなたのコードはちょうど 'p1.start()'と 'progress_bar()'を実行するのと同じです第2のプロセスを無意味にする主なプロセス。第3のプロセスを追加して、私が何を意味するのかを試すことができます。 – Bahrom

関連する問題