So I have a question I have a project where I will be uses threads to work on databases. So I wrote a small snippet code just to test how the library works. Fairly new to using python and threads. So sometimes the print will print the output correct and other times it will print the output weird.
So I am passing in a list of numbers 2,3,4,9 and one function squares each number and print it and the other cubes each number then prints.
it is supposed to print out mix up of a squared number and a cubed number but sometimes it prints two numbers on the same line. And when I used strings like print('squared number: ', n*n) the letters get all mixed up. I stopped using print('suared number: ',n*n) and instead just use print(n*n). It is easier to see the output
So basically it is not executed the entire print function in the loop of each function.
if it does this in my project it will insert part of the values in the database instead of a full row and cause an error.
So sometimes the output is correct like below:
calculate square numbers
4
9
64
81
calculate cube of numbers
8
27
512
729
('done in : ', 1.6080000400543213)
Done with all the functions now!
Now time with the threads
('done in : ', 0.80765430)
calculate square numberscalculate cube of numbers
4
8
9
27
64
512
81
729
And other times it is like this:
calculate square numbers
4
9
64
81
calculate cube of numbers
8
27
512
729
('done in : ', 1.6069998741149902)
Done with all the functions now!
Now time with the threads
('done in : ', 0.0)
calculate square numberscalculate cube of numbers
84
27
9
512
64
729
81
Actually code below
So I am passing in a list of numbers 2,3,4,9 and one function squares each number and print it and the other cubes each number then prints.
it is supposed to print out mix up of a squared number and a cubed number but sometimes it prints two numbers on the same line. And when I used strings like print('squared number: ', n*n) the letters get all mixed up. I stopped using print('suared number: ',n*n) and instead just use print(n*n). It is easier to see the output
So basically it is not executed the entire print function in the loop of each function.
if it does this in my project it will insert part of the values in the database instead of a full row and cause an error.
So sometimes the output is correct like below:
calculate square numbers
4
9
64
81
calculate cube of numbers
8
27
512
729
('done in : ', 1.6080000400543213)
Done with all the functions now!
Now time with the threads
('done in : ', 0.80765430)
calculate square numberscalculate cube of numbers
4
8
9
27
64
512
81
729
And other times it is like this:
calculate square numbers
4
9
64
81
calculate cube of numbers
8
27
512
729
('done in : ', 1.6069998741149902)
Done with all the functions now!
Now time with the threads
('done in : ', 0.0)
calculate square numberscalculate cube of numbers
84
27
9
512
64
729
81
Actually code below
Code:
#!/usr/bin/python
import psycopg2
import time
import threading
def calc_square(numbers):
print("calculate square numbers")
for n in numbers:
time.sleep(0.2)
#print('squared number: ',n*n)
print(n*n)
def calc_cube(numbers):
print("calculate cube of numbers")
for n in numbers:
time.sleep(0.2)
#print('cube number: ',n*n*n)
print(n*n*n)
arr = [2,3,8,9]
t = time.time()
calc_square(arr)
calc_cube(arr)
print("done in : ",time.time()-t)
print("Done with all the functions now!")
print("")
print("")
print("Now time with the threads")
s = time.time()
print("done in : ",time.time()-s)
t1 = threading.Thread(target=calc_square,args=(arr,))
t2 = threading.Thread(target=calc_cube,args=(arr,))
t1.start()
t2.start()
t1.join()
t2.join()