Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import threading
- class PrintFactorial(threading.Thread):
- def __init__(self, n):
- super(PrintFactorial, self).__init__() # call the original threading.Thread constructor
- self.n = n # user-provided n value whose factorial value will be calculated in a new thread
- def run(self):
- """The 'run' method is a classmethod that will be run within a new thread.
- Because the return value is not stored, we are printing the value to stdout."""
- val = 1
- while self.n > 1:
- val *= self.n
- self.n -= 1
- print(val)
- pf = PrintFactorial(33733) # creates a new instance of the above class
- pf.start() # executes the above run method in a new thread
- print("Calculating... Awaiting ") # this occurs in the main thread
- pf.join() # blocks execution of the main thread until the run function is complete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement