Книга: Learning Concurrency in Python
Назад: Exception classes
Дальше: Output

Example

In this example, we'll define an isEven function, which takes in a value. Within this function, it first does a check to see if the type of the object passed into it is an integer. If it isn't, then it raises a new exception.

After this type check, it then checks to see if the number is even or not. If it is even, then it returns true, and if not, it returns false:

from concurrent.futures import ThreadPoolExecutor
import concurrent.futures
import threading
import random

def isEven(n):
print("Checking if {} is even".format(n))
if type(n) != int:
raise Exception("Value entered is not an integer")
if n % 2 == 0:
print("{} is even".format(n))
return True
else:
print("{} is odd".format(n))
return False

def main():
with ThreadPoolExecutor(max_workers=4) as executor:
task1 = executor.submit(isEven, (2))
task2 = executor.submit(isEven, (3))
task3 = executor.submit(isEven, ('t'))

for future in concurrent.futures.as_completed([task1, task2, task3]):
print("Result of Task: {}".format(future.result()))
if __name__ == '__main__':
main()
Назад: Exception classes
Дальше: Output