Last active
September 16, 2020 05:42
-
-
Save pythonlessons/2daa9b5f3b69f9bed62fb615ed9102c6 to your computer and use it in GitHub Desktop.
multiprocessing_pipe_numpy
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, Pipe | |
| import time | |
| import numpy as np | |
| def send_to_pipe(q, mylist): | |
| # function to put elements into Pipe | |
| for num in mylist: | |
| q.send(np.random.random((416, 416, 3))) | |
| def pick_from_pipe(q, mylist): | |
| # function to print pipe elements | |
| print("Pipe elements:") | |
| t1 = time.time() | |
| for num in mylist: | |
| result = q.recv() | |
| #print(result) | |
| print("Pipe is now empty!", time.time()-t1) | |
| if __name__ == "__main__": | |
| # input list | |
| mylist = [i for i in range(100)] | |
| # creating multiprocessing Pipe | |
| parent_conn, child_conn = Pipe() | |
| # creating new processes | |
| p1 = Process(target=send_to_pipe, args=(parent_conn, mylist)) | |
| p2 = Process(target=pick_from_pipe, args=(child_conn, 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