在 python 中使用多线程打印模式中的值
Printing Vaules in a pattern using multi-threading in python
目标是打印:
斐波那契数:
斐波纳奇平方:
质数:
质数的平方:
使用多线程以最多前 10 个值的模式。
这是我的代码:
import threading
import time
def FibonacciNumbers(n):
f1 = 0
f2 = 1
if (n < 1):
return
for x in range(0, n):
print("Fibonacci No.: ")
print(f2)
time.sleep(1)
next = f1 + f2
f1 = f2
f2 = next
def FibonacciSq(n):
f1 = 0
f2 = 1
if (n < 1):
return
for x in range(0, n):
print("Square of Fibo: ")
print(f2*f2)
time.sleep(1)
next = f1 + f2
f1 = f2
f2 = next
def prime(x):
i=1
counter = 0
while True:
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print("Prime No: ")
print (i)
time.sleep(1)
counter = counter + 1
if counter >= x:
break
i=i+1
def primeSq(x):
i=1
counter = 0
while True:
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print("Square of the Prime Number: ")
print (i*i)
time.sleep(1)
counter = counter + 1
if counter >= x:
break
i=i+1
if __name__ == "__main__":
t1 = threading.Thread(target=FibonacciNumbers, args=(10,))
t2 = threading.Thread(target=FibonacciSq, args=(10,))
t3 = threading.Thread(target=prime, args=(10,))
t4 = threading.Thread(target=primeSq, args=(10,))
t1.start()
time.sleep(1)
# t1.join()
t2.start()
time.sleep(1)
# t2.join()
t3.start()
time.sleep(1)
# t3.join()
t4.start()
time.sleep(1)
# t4.join()
t1.join()
t2.join()
t3.join()
t4.join()
print("Done!")
我得到的输出是:
输出是随机生成的。
我认为这是由于在生成结果的时候发生了一些碰撞。
无法弄清楚如何暂停一个线程,直到下一个线程完成。
请帮助我修复输出。
Fibonacci No.:
1
Fibonacci No.:
1
Square of Fibo:
1
Fibonacci No.:
2
Square of Fibo:
1
Prime No:
2
34
Square of Fibo:
169
441
Prime No:
Square of the Prime Number:
289
Fibonacci No.:
19
55
Square of Fibo:
1156
Square of Fibo:
Square of the Prime Number:
361
3025
Prime No:
23
Square of the Prime Number:
529
Prime No:
29
Square of the Prime Number:
841
Done!
当多个线程并发访问单个资源(在本例中为标准输出)时,您需要以某种方式同步访问(意思是:让除一个线程外的所有线程等待资源再次可用)。
有多种解决方案,具体取决于 use-case。最通用的解决方案是使用 lock。这需要两个更改:
在文件开头声明一个全局锁:
lock = threading.Lock()
在每个函数中,将两个 print
语句包装在一个锁定的块中,例如:
with lock:
print("Fibonacci No.: ")
print(f2)
请注意,以这种方式锁定是一种非常低效的操作!如果使用并发的目的是提高性能,那么这个解决方案可能会完全违背这个目的。编写高效的 multi-threaded 代码绝非易事,而高效的同步尤其需要大量的时间。 (有更多的复杂性,因为最常见的 Python 的实现使用 global interpreter lock,这意味着大部分执行不会 真的 并行发生无论如何。)
在大多数情况下,最简单实用、高效的解决方案是避免线程之间共享资源。在你的情况下,这意味着不写任何输出(写输出的函数通常是可疑的):让每个函数生成结果并将它们收集在一个列表中,或者 yield
它们作为生成器。然后你可以并行生成结果,并在之后顺序打印它们(这样做需要改变,因为threading
不容易支持return值的函数;但是,模块 concurrent.futures
可以)。
目标是打印:
斐波那契数:
斐波纳奇平方:
质数:
质数的平方:
使用多线程以最多前 10 个值的模式。
这是我的代码:
import threading
import time
def FibonacciNumbers(n):
f1 = 0
f2 = 1
if (n < 1):
return
for x in range(0, n):
print("Fibonacci No.: ")
print(f2)
time.sleep(1)
next = f1 + f2
f1 = f2
f2 = next
def FibonacciSq(n):
f1 = 0
f2 = 1
if (n < 1):
return
for x in range(0, n):
print("Square of Fibo: ")
print(f2*f2)
time.sleep(1)
next = f1 + f2
f1 = f2
f2 = next
def prime(x):
i=1
counter = 0
while True:
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print("Prime No: ")
print (i)
time.sleep(1)
counter = counter + 1
if counter >= x:
break
i=i+1
def primeSq(x):
i=1
counter = 0
while True:
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print("Square of the Prime Number: ")
print (i*i)
time.sleep(1)
counter = counter + 1
if counter >= x:
break
i=i+1
if __name__ == "__main__":
t1 = threading.Thread(target=FibonacciNumbers, args=(10,))
t2 = threading.Thread(target=FibonacciSq, args=(10,))
t3 = threading.Thread(target=prime, args=(10,))
t4 = threading.Thread(target=primeSq, args=(10,))
t1.start()
time.sleep(1)
# t1.join()
t2.start()
time.sleep(1)
# t2.join()
t3.start()
time.sleep(1)
# t3.join()
t4.start()
time.sleep(1)
# t4.join()
t1.join()
t2.join()
t3.join()
t4.join()
print("Done!")
我得到的输出是:
输出是随机生成的。 我认为这是由于在生成结果的时候发生了一些碰撞。 无法弄清楚如何暂停一个线程,直到下一个线程完成。 请帮助我修复输出。
Fibonacci No.:
1
Fibonacci No.:
1
Square of Fibo:
1
Fibonacci No.:
2
Square of Fibo:
1
Prime No:
2
34
Square of Fibo:
169
441
Prime No:
Square of the Prime Number:
289
Fibonacci No.:
19
55
Square of Fibo:
1156
Square of Fibo:
Square of the Prime Number:
361
3025
Prime No:
23
Square of the Prime Number:
529
Prime No:
29
Square of the Prime Number:
841
Done!
当多个线程并发访问单个资源(在本例中为标准输出)时,您需要以某种方式同步访问(意思是:让除一个线程外的所有线程等待资源再次可用)。
有多种解决方案,具体取决于 use-case。最通用的解决方案是使用 lock。这需要两个更改:
在文件开头声明一个全局锁:
lock = threading.Lock()
在每个函数中,将两个
print
语句包装在一个锁定的块中,例如:with lock: print("Fibonacci No.: ") print(f2)
请注意,以这种方式锁定是一种非常低效的操作!如果使用并发的目的是提高性能,那么这个解决方案可能会完全违背这个目的。编写高效的 multi-threaded 代码绝非易事,而高效的同步尤其需要大量的时间。 (有更多的复杂性,因为最常见的 Python 的实现使用 global interpreter lock,这意味着大部分执行不会 真的 并行发生无论如何。)
在大多数情况下,最简单实用、高效的解决方案是避免线程之间共享资源。在你的情况下,这意味着不写任何输出(写输出的函数通常是可疑的):让每个函数生成结果并将它们收集在一个列表中,或者 yield
它们作为生成器。然后你可以并行生成结果,并在之后顺序打印它们(这样做需要改变,因为threading
不容易支持return值的函数;但是,模块 concurrent.futures
可以)。