Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##################################################################################################
- import multiprocessing
- import time
- import os
- # PROBLEM: WHEN DEFINED HERE THEN IT IT WORKS
- semaphore = multiprocessing.Semaphore(1)
- def job(num, output):
- semaphore.acquire()
- time.sleep(1)
- element = "PROCESS: %d PID: %d PPID: %d" % (num, os.getpid(), os.getppid())
- print "WRITE -> " + element
- output.put(element)
- time.sleep(1)
- semaphore.release()
- if __name__ == '__main__':
- """
- Reads elements as soon as they are are put inside queue
- """
- output = multiprocessing.Manager().Queue()
- pool = multiprocessing.Pool(4)
- # # PROBLEM: WHEN DEFINED HERE THEN IT DOES NOT WORKS
- # semaphore = multiprocessing.Semaphore(1)
- lst = range(40)
- for i in lst:
- pool.apply_async(job, (i, output))
- print "%d Do not wait!" % i
- # res.get()
- counter = 0
- while True:
- try:
- print "READ <- " + output.get_nowait()
- counter += 1
- if (counter == len(lst)):
- print "Break"
- break
- except:
- print "READ <- NOTHING IN BUFFER"
- pass
- time.sleep(1)
- pool.close()
- pool.join()
- ##################################################################################################
- # PRINT 10
- global_var = 20
- def print_global_var():
- print global_var
- if __name__ == '__main__':
- global_var = 10
- print_global_var()
- ##################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement