Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Created March 8, 2023 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liviaerxin/0735469478eef6b27ac6fb58b2ebb85f to your computer and use it in GitHub Desktop.
Save liviaerxin/0735469478eef6b27ac6fb58b2ebb85f to your computer and use it in GitHub Desktop.
Measure Multiple Process/Thread Queue #python
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
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