How to get count of threads from user and start Thread Python - python

I can start function with my own count of threads and it works:
start_time = time.time()
t1 = Thread(target=time.sleep, args=(3, ))
t2 = Thread(target=time.sleep, args=(3, ))
t1.start()
t2.start()
t1.join()
t2.join()
print("--- %s seconds ---" % (time.time() - start_time))
Output:
--- 3.00131893157959 seconds ---
but I want to input threads number from user, I tried to do this:
start_time = time.time()
threads_number = int(input('Input count of threads: ')) # User inputed 2
threads = list(range(0, 99999))
for i in range(0, threads_number):
threads[i] = Thread(target=time.sleep, args=(3, ))
threads[i].start()
threads[i].join()
print("--- %s seconds ---" % (time.time() - start_time))
Output:
--- 7.817119359970093 seconds ---
How to make last output 3 seconds?

Before :
t1.start()
t2.start()
t1.join()
t2.join()
Both thread start, then wait for both threads to finish (join).
After :
for i in range(0, threads_number):
threads[i] = Thread(target=time.sleep, args=(3, ))
threads[i].start()
threads[i].join()
First thread starts, then is waited to finish, then second thread starts, then is waited to finish, ...
Solution : start all threads, then wait all threads.
for i in range(0, threads_number):
threads[i] = Thread(target=time.sleep, args=(3, ))
threads[i].start()
for i in range(0, threads_number):
threads[i].join()
Runs in ~3 seconds (if fast to write the number).

Related

Python mutithreadding, diffrent execution timings

HI I have made a python code to check its multi threading capabilities of the code
here is my code
import os
import time
import threading
count = 0
number_of_itration =10000000
def print_square(num):
# function to print square of given num
begin = time.time()
print(f"T1Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T1End Time is {end} Seconds\n")
print(f"T1Total runtime of the program is {end - begin} Seconds\n")
def print_square1(num):
# function to print square of given num
begin = time.time()
print(f"T2Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T2End Time is {end} Seconds\n")
print(f"T2Total runtime of the program is {end - begin} Seconds\n")
def print_square2(num):
# function to print square of given num
begin = time.time()
print(f"T3Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T3End Time is {end} Seconds\n")
print(f"T3Total runtime of the program is {end - begin} Seconds\n")
def print_square3(num):
# function to print square of given num
begin = time.time()
print(f"T4Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T4End Time is {end} Seconds\n")
print(f"T4Total runtime of the program is {end - begin} Seconds\n")
def print_square4(num):
# function to print square of given num
begin = time.time()
print(f"T5Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T5End Time is {end} Seconds\n")
print(f"T5Total runtime of the program is {end - begin} Seconds\n")
def print_square5(num):
# function to print square of given num
begin = time.time()
print(f"T6Begin Time is {begin} Seconds\n")
for i in range(number_of_itration):
format(num * num * num)
end = time.time()
print(f"T6End Time is {end} Seconds\n")
print(f"T6Total runtime of the program is {end - begin} Seconds\n")
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_square1, args=(10,))
t3 = threading.Thread(target=print_square2, args=(10,))
t4 = threading.Thread(target=print_square3, args=(10,))
t5 = threading.Thread(target=print_square4, args=(10,))
t6 = threading.Thread(target=print_square5, args=(10,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
here Though im calling the same function, with same process, im getting different execution speeds why does it behave like this?
the console print is displayed below
T1Begin Time is 1660808471.2426443 Seconds
T2Begin Time is 1660808471.256599 Seconds
T3Begin Time is 1660808471.256599 Seconds
T4Begin Time is 1660808471.6522112 Seconds
T5Begin Time is 1660808472.0985467 Seconds
T6Begin Time is 1660808472.6798558 Seconds
T2End Time is 1660808478.985982 Seconds
T2Total runtime of the program is 7.7293829917907715 Seconds
T1End Time is 1660808480.8472064 Seconds
T1Total runtime of the program is 9.604562044143677 Seconds
T5End Time is 1660808481.6313431 Seconds
T5Total runtime of the program is 9.532796382904053 Seconds
T3End Time is 1660808482.2941368 Seconds
T6End Time is 1660808482.3155725 Seconds
T6Total runtime of the program is 9.635716676712036 Seconds
T3Total runtime of the program is 11.037537813186646 Seconds
T4End Time is 1660808482.4218156 Seconds
T4Total runtime of the program is 10.769604444503784 Seconds
I would like to know if its expected or not?
I would say that this is unsurprising*. Your program is using one CPU core and you are adding more and more work for the one core as you call .start() on your threads. So you would expect later threads to have some more competition for CPU resources.
* I wouldn't say "expected" purely because performance measurements are hard and lots of unexpected things do crop up.

Python thread runs only once instead of running continuously. All threads should run continuously

I'm trying to implement threading but it only runs once instead of running continuously.
This is where I'm creating the threads and calling the functions :
thread_list = []
for ldx, stream in enumerate(input_stream_list):
thread = threading.Thread(target=frame_thread, args=(stream, "stream{0}".format(ldx), ldx))
thread_list.append(thread)
for thread in thread_list:
thread.start()
retrieve_thread_list = []
for kdx, streamk in enumerate(input_stream_list):
threadk = threading.Thread(target=retrieve_thread, args=(streamk, kdx))
retrieve_thread_list.append(threadk)
for threadk in retrieve_thread_list:
threadk.start()
for thread in thread_list:
thread.join()
for threadk in retrieve_thread_list:
threadk.join()
while True:
pass
This is the implementation of the frame_thread :-
def frame_thread(url, dire, i):
global camera_reconnection, video_retrieval_status
cap = cv2.VideoCapture(url)
ret = cap.set(3, 768)
ret = cap.set(4, 432)
if not os.path.exists(name + dire):
os.mkdir(name + dire)
for x in os.listdir(name+dire):
items[i].append(x)
items[i].sort()
for k in items[i]:
q2[i].append(os.path.join(name+dire, k))
while cap.isOpened():
if camera_reconnection[i]:
cap = cv2.VideoCapture(url)
last_frame[i] = False
camera_reconnection[i] = False
if last_frame[i] is False:
start_time = time.time()
ret, frame = cap.read()
if ret:
video_file = os.path.join(name + dire, str(time.strftime('%H %M %S'))+".avi")
q2[i].append(video_file)
if len(q2[i]) > int(video_save):
data = q2[i].popleft()
os.remove(data)
video_writer = cv2.VideoWriter(video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
while(int(time.time() - start_time)) < video_duration:
ret, frame = cap.read()
if not ret:
last_frame[i] = True
video_writer.release()
cv2.destroyAllWindows()
break
video_writer.write(frame)
video_writer.release()
if(video_retrieval_status[i] == VIDEO_LIST_RETRIEVAL_SPECIFIC_START):
video_retrieval_status[i] = VIDEO_LIST_RETRIEVAL_SPECIFIC_READY
And Here's my retrieve_thread function : -
def retrieve_thread(streamk, k):
global video_retrieval_status
print("dbg0", flush=True)
if(video_retrieval_status[k] == NO_RETRIEVAL):
print("dbg1", flush=True)
else:
print("dbg2", flush=True)
if (video_retrieval_status[k] == VIDEO_LIST_RETRIEVAL_SPECIFIC_READY):
print("dbg3", flush=True)
post_storage_retrieval_specific(k)
print("dbg4", flush=True)
video_retrieval_status[k] = NO_RETRIEVAL
The threadk - retrieve_thread is only executing once. What am I doing wrong?
retrieve_thread() does not have any loop. Once the function ends, the thread will close.
Try adding a while loop:
import time
def retrieve_thread(streamk, k):
global video_retrieval_status
while True: # Any condition to continue the thread execution
time.sleep(5) # Make sure the polling isn't too frequent
print("dbg0", flush=True)
if(video_retrieval_status[k] == NO_RETRIEVAL):
print("dbg1", flush=True)
else:
print("dbg2", flush=True)
if (video_retrieval_status[k] == VIDEO_LIST_RETRIEVAL_SPECIFIC_READY):
print("dbg3", flush=True)
post_storage_retrieval_specific(k)
print("dbg4", flush=True)
video_retrieval_status[k] = NO_RETRIEVAL

Video stream slow # receiver side when processing

import time
import threading
import cv2
import imagezmq
import cv2
from flask import Flask, Response
import emotion_detection_copy, mouth_open_copy
import objectdetection
import final_recogcopy
import predict
import person_and_phone_copy, head_pose_copy
import eye_tracker_copy
import predict
import datetime
image_hub = imagezmq.ImageHub()
while True:
cam_id, frame = image_hub.recv_image()
print("before", datetime.datetime.now())
# predict.predict_labels(frame) # working fine
t1 =
threading.Thread(target=final_recogcopy.recog(frame),args())
t2 =
threading.Thread(target=objectdetection.object_detection(frame), args=()) # working fine
t3 = threading.Thread(target=emotion_detection_copy.detect_emotion(frame), args=()) # working fine
t4 = threading.Thread(target=person_and_phone_copy.person_and_phone_count(frame), args=()) # working fine
t5 = threading.Thread(target=head_pose_copy.head_position(frame), args=()) # working fine cv2.waitKey(1)
t6 = threading.Thread(target=eye_tracker_copy.gaze_detection(frame))
final_recogcopy.recog(frame)
t7 = threading.Thread(target=mouth_open_copy.mouth_opening_detection(frame))
x = datetime.datetime.now()
#print(start)
t2.start()
start1 = time.time()
t3.start()
start2 = time.time()
t4.start()
start3 = time.time()
t5.start()
start4 = time.time()
t6.start()
start5 = time.time()
t1.start()
start = time.time()
# t7.start()
t1.join()
y = datetime.datetime.now()
print("diff is", y - x)
end = time.time()
print(end)
print("Total time taken T1", start - end)
t2.join()
end1 = time.time()
print("Total time taken T2", start1 - end1)
t3.join()
end2 = time.time()
print("Total time taken T3", start2 - end2)
t4.join()
end3 = time.time()
print("Total time taken T4", start3 - end3)
t5.join()
end4 = time.time()
print("Total time taken T5", start4 - end4)
t6.join()
end5 = time.time()
print("Total time taken T6", start5 - end5)
# t7.join()
print("after", datetime.datetime.now())
cv2.imshow(cam_id, frame)
cv2.waitKey(1)
image_hub.send_reply(b'OK')
I am using imagezmq for streaming of video in python from my webcam using opencv. when i am streaming normal video to the receiver side its normal at receiver side but when processing like object detection etc at receiver side the video will not like near to realtime its very messy and slow . plzzzz help.
Thanks

Python single thread is slower than two thread in CPU bound

I think a thread is faster than two threads in CPU bound.
So, I did an thread experiment.
I executed the CPU bound program 5 times in macOS Mojave Python 3.7.
The code like this.
1 thread
import time
COUNT = 50000000
def countdown(n):
while n>0:
n -= 1
start = time.time()
countdown(COUNT)
end = time.time()
print('Time taken in seconds -', end - start)
#2.202889919281006
#2.2208809852600098
#2.2509100437164307
#2.1846389770507812
#2.2133240699768066
#avg: 2.214528799
2 threads
import time
from threading import Thread
COUNT = 50000000
def countdown(n):
while n > 0:
n -= 1
t1 = Thread(target=countdown, args=(COUNT // 2,))
t2 = Thread(target=countdown, args=(COUNT // 2,))
start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()
print('Time taken in seconds -', end - start)
#2.1756350994110107
#2.184033155441284
#2.1663358211517334
#2.2320470809936523
#2.2731218338012695
#avg: 2.206234598
4 threads
# ....
t1 = Thread(target=countdown, args=(COUNT // 4,))
t2 = Thread(target=countdown, args=(COUNT // 4,))
t3 = Thread(target=countdown, args=(COUNT // 4,))
t4 = Thread(target=countdown, args=(COUNT // 4,))
start = time.time()
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
end = time.time()
print('Time taken in seconds -', end - start)
#2.1956586837768555
#2.218824863433838
#2.3495471477508545
#2.388563871383667
#2.186440944671631
#avg: 2.267807102
I think a thread is faster than multi threads because of GIL.(GIL lock & release, thread context switching)
but, 2 threads is faster than a thread. Is wrong my code??

Why is the task slower after adding multiple processes?

I want to add multiple processes to speed up my program, but I found that after adding multiple processes, the program execution time has become longer.My code is as follows.
'''before'''
if __name__ == '__main__':
result = []
start_time = int(time.time())
for i in range(20000000):
result.append(demo3(i, i + 1))
end_time = int(time.time())
print(result)
print(end_time - start_time)
'''Add multiple processes '''
def demo3(i, j):
return int(i) * int(j)
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=10)
result = []
start_time = int(time.time())
for i in range(20000000):
result.append(pool.apply_async(demo3, args=(i, i + 1)).get())
pool.close()
pool.join()
end_time = int(time.time())
print(result)
print(end_time - start_time)

Categories

Resources