Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[신윤석] W2M3 #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added missions/.DS_Store
Binary file not shown.
Binary file added missions/W2/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions missions/W2/M3/M3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import multiprocessing as mp

# constants
COLOR = ['red', 'green', 'blue', 'black']

# working function
def work(q:mp.Queue, iQ:mp.Queue, color:str):
i = iQ.get() # get semaphore

# processing: put color to the Queue
q.put(color)
print(f"item no: {i} {color}")

iQ.put(i+1) # return semaphore

if __name__ == "__main__":
proc = [] # processes
queue = mp.Queue() # Color Queue
numQ = mp.Queue() # number Queue; used like a semaphore

numQ.put(1) # initialize number Queue

# processes start
print("pushing items to queue:")

for i in range(len(COLOR)):
p = mp.Process(target=work, args=(queue, numQ, COLOR[i]))
p.start()
proc.append(p)

# wait for processes
for p in proc:
p.join()

# print result
print("popping items form queue:")

i = 0
while not queue.empty():
print(f"item no: {i} {queue.get()}")
i += 1