Created
March 8, 2023 08:37
-
-
Save liviaerxin/0735469478eef6b27ac6fb58b2ebb85f to your computer and use it in GitHub Desktop.
Measure Multiple Process/Thread Queue #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Queue | |
from multiprocessing import Process | |
import time | |
import os | |
import numpy as np | |
def put_into_queue(q: Queue): | |
print(f"PID[{os.getpid()}] put_into_queue") | |
img = np.zeros((1024, 1920, 3), np.uint8) | |
for i in range(10): | |
start = time.time() | |
q.put(img) | |
end = time.time() | |
print(f"PID[{os.getpid()}] putting time: {(end - start)*1000}ms") | |
print(f"put finish") | |
def get_from_queue(q: Queue): | |
print(f"PID[{os.getpid()}] get_from_queue") | |
for i in range(10): | |
start = time.time() | |
img = q.get() | |
end = time.time() | |
print(f"PID[{os.getpid()}] getting time: {(end - start)*1000}ms") | |
if __name__ == "__main__": | |
print(f"PID[{os.getpid()}]") | |
q = Queue(1000) | |
p1 = Process(target=put_into_queue, args=(q,)) | |
p2 = Process(target=get_from_queue, args=(q,)) | |
p1.start() | |
p2.start() | |
p1.join() | |
p2.join() | |
# get_from_queue(q) #from main thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
import time | |
import os | |
import numpy as np | |
from queue import Queue | |
def put_into_queue(q: Queue): | |
print(f"PID[{os.getpid()}] put_into_queue") | |
img = np.zeros((1024, 1920, 3), np.uint8) | |
for i in range(10): | |
start = time.time() | |
q.put(img) | |
end = time.time() | |
print(f"PID[{os.getpid()}] putting time: {(end - start)*1000}ms") | |
print(f"put finish") | |
def get_from_queue(q: Queue): | |
print(f"PID[{os.getpid()}] get_from_queue") | |
for i in range(10): | |
start = time.time() | |
img = q.get() | |
end = time.time() | |
print(f"PID[{os.getpid()}] getting time: {(end - start)*1000}ms") | |
if __name__ == "__main__": | |
print(f"PID[{os.getpid()}]") | |
q = Queue(1000) | |
p1 = Thread(target=put_into_queue, args=(q,)) | |
p2 = Thread(target=get_from_queue, args=(q,)) | |
p1.start() | |
p2.start() | |
p1.join() | |
p2.join() | |
# get_from_queue(q) #from main thread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment