in the below code i am trying to learn how to use threads with synchronization. as shown below in the code i generate a thread every 3 seconds an a random number is passed to the method isNumPrime(...) to check if the generated number is prime or not. the problems i am facing are
1-the output i receive every time i run the code is the same. please have a look at the output below. the same random numbers are generated every time i press ctrl+f5 is
thread_1 = threading.Thread(group = None, target = self.isNumPrime,
name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)),
daemon = None)
indeed generates random nmbers`?? because what i am getting is not randomly generated numbers. i expect to receive differently generated numbers each time i press ctrl+f5
2-given the output belwo, why i am receiving repeated output of "in loop: targetNum: xx"?? it must be displayed once for each single iteration with a number from range of (3,targetNum)
code:
import threading
import logging
import time
from random import seed
from random import randint
class ThreadsWithSync(threading.Thread):
def __new__(cls):
"""
For object creation
"""
print("cls: %s"%(cls))
#cls.onCreateObject()
instance = super(ThreadsWithSync, cls).__new__(cls)
#print("instace: %s"%(instance.__repr__)) #activate this line whenever an informative and descriprtive text about the instance is needed to be displayed
return instance
def __init__(self):
"""
For object initialization
"""
#print("self: %s"%(self))
threading.Thread.__init__(self) #to initialize the super class
print("self: %s"%(self))
seed(1)
@classmethod
def onCreateObject(cls):
"""
This will be invoked once the creation procedure of the object begins.
"""
def __repr__(self):
"""
similar to toString in Java
"""
return "\n__class__: " + repr(self.__class__) +"\n__new__:" + repr(self.__new__) + "\n__str__: " + repr(self.__str__) + "\n__sizeof__: " + repr(self.__sizeof__)
def isNumPrime(self, targetNum):
if targetNum == 0 or targetNum == 1:
print("targetNum passed to method, will return false: %s"%(targetNum))
return False
if targetNum == 2:
print("targetNum passed to method will return true: %s"%(targetNum))
return True
isPrim = True
for i in range(3,targetNum):
print("in loop: targetNum: %s"%(targetNum))
if targetNum % i == 0:
isPrim = False
break
print("is %s a prime : %s"%(targetNum, isPrim))
return isPrim
def spawnThread(self):
if __name__ == "__main__":
self.main()
def main(self):
while True:
thread_1 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
#thread_2 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_2', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
thread_1.start()
#thread_2.start()
time.sleep(3)
t1 = ThreadsWithSync()
t1.spawnThread()
output:
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
is 17 a prime : True
in loop: targetNum: 72
is 72 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 8
in loop: targetNum: 8
is 8 a prime : False
in loop: targetNum: 32
in loop: targetNum: 32
is 32 a prime : False
in loop: targetNum: 15
is 15 a prime : False
in loop: targetNum: 63
is 63 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
...
...
...
...
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 57
Aucun commentaire:
Enregistrer un commentaire