December 2018
Beginner to intermediate
796 pages
19h 54m
English
First things first, let's start a thread:
# start.pyimport threadingdef sum_and_product(a, b): s, p = a + b, a * b print(f'{a}+{b}={s}, {a}*{b}={p}')t = threading.Thread( target=sum_and_product, name='SumProd', args=(3, 7))t.start()
After importing threading, we define a function: sum_and_product. This function calculates the sum and the product of two numbers, and prints the results. The interesting bit is after the function. We instantiate t from threading.Thread. This is our thread. We passed the name of the function that will be run as the thread body, we gave it a name, and passed the arguments 3 and 7, which will be fed into the function as a and b, respectively.
After having created the thread, we start it with the ...
Read now
Unlock full access