Created
September 15, 2020 13:57
-
-
Save pythonlessons/1a8d8d2ea55bb75c56f0cf39dfbaba20 to your computer and use it in GitHub Desktop.
multiprocessing_queue_numpy.py
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 Process, Queue | |
| import time | |
| import numpy as np | |
| def send_to_queue(q, mylist): | |
| # function to put elements into Queue | |
| for num in mylist: | |
| q.put(np.random.random((416, 416, 3))) | |
| def pick_from_queue(q, mylist): | |
| # function to print queue elements | |
| print("Queue elements:") | |
| t1 = time.time() | |
| for num in mylist: | |
| result = q.get() | |
| #print(result) | |
| print("Queue is now empty!", time.time()-t1) | |
| if __name__ == "__main__": | |
| # input list | |
| mylist = [i for i in range(100)] | |
| # creating multiprocessing Queue | |
| q = Queue() | |
| # creating new processes | |
| p1 = Process(target=send_to_queue, args=(q, mylist)) | |
| p2 = Process(target=pick_from_queue, args=(q, mylist)) | |
| # running process p1 and p2 | |
| p1.start() | |
| p2.start() | |
| # if we need waiting p1 and p2 process to finish their job | |
| #p1.join() | |
| #p2.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment