
pythonのスレッド間で変数の受け渡し方法がわからず困っています。
やりたいことは、非同期で動くスレッド1で計算する数値xと、スレッド2で計算する数値yを別のスレッドで合算することなのですが、うまくできません。自分で下記のように書いてみたのですが、機能しませんでした。x_score関数とy_score関数で計算した数値を、z_scoreでどうやって取得できるのでしょうか?ネットで探してもぴったりの解説がなく、行き詰ってしまったので質問いたします。どなたか教えていただけると幸いです。よろしくお願いいたします。
python
1import threading 2import time 3 4 5def z_score(x,y): 6 zx=0 7 zy=0 8 if x>0 and y==0: 9 zx=x 10 if x==0 and y<0: 11 zy=y 12 z=zx+zy 13 print(z) 14 time.sleep(1) 15 16th_3 = threading.Thread(target=z_score) 17th_3.start() 18 19def x_score(): 20 x=1 21 while True: 22 x=x+1 23 z_score(x,0) 24 time.sleep(1) 25 26th_1 = threading.Thread(target=x_score) 27th_1.start() 28 29def y_score(): 30 y=-1 31 while True: 32 y=y-2 33 z_score(0,y) 34 time.sleep(2) 35 36th_2 = threading.Thread(target=y_score) 37th_2.start() 38
回答2件
あなたの回答
tips
プレビュー