私はマルチプロセッシングが私にとって新しいものだと言います。私はそれについていくつか読んだが、それは私をもっと混乱させる。私は簡単な例でそれを理解したい。最初の関数に2つの関数があると仮定しましょう。変数 'a'をインクリメントして 'number'変数に代入します。次に、最初に関数を開始し、1秒ごとに 'number'変数を出力します。次のようになります。Pythonでの並列処理の簡単な例
global number
def what_number():
a=1
while True:
a+=1
number=a
def read_number():
while True:
--> #here I need to start 'what_number' function <--
time.sleep(1)
print(number)
if __name__ == "__main__":
read_number()
どうすればいいですか?それを行うための簡単かつ適切な方法はありますか?
UPDATE:
私は本当にthankfullだが、それは私がしたい正確に何でないnoxdafox答えを見ました。まず第一に、私は最初の関数(noxdafoxコードの 'main')に値を送ることは望ましくありません。二番目に私はすべての価値を得たくないので、クーネはうまくいかないでしょう。 whileループの2番目のそれぞれの後に取得する必要があります。 uは上記のコードを実行した場合、あなたが何かを得る
import multiprocessing
import time
number = 0
def child_process():
global number
while True:
number += 1
print(number)
def main():
process = multiprocessing.Process(target=child_process)
process.start()
while True:
print("should get same number:",number)
time.sleep(0.001)
if __name__ == "__main__":
main()
をしかし、この青の値が同じである必要があり、選択したコードのようなものでなければなりません!それが一番の問題だ:)混沌
http://chriskiehl.com/article/parallelism-in-one-line/ – user45245
グローバル番号? – alfasin
[multiprocessing.Pool:applyを使用する場合は、\ _asyncまたはmapを適用しますか?](https://stackoverflow.com/questions/8533318/multiprocessing-pool-when-to-use-apply-apply-async-または – alfasin