2014-11-27 1 views
0

두 개의 프로세스를 만들고 있습니다. 1에서 5까지 Mutliprocessing.Queue에 int 값을 추가합니다. 다른 하나는 Queue에 한 번 더 추가 한 다음 10 초 동안 반복하고 Queue에서 값을 읽으려고 시도합니다.Python 다중 처리 대기열에서 대기열 항목을 공유하지 않습니다.

두 번째 프로세스가 첫 번째 스레드에서 항목을 대기열에 추가하지 못하는 이유는 무엇입니까? 나는 Multiprocessing.Queue를 만들 때 한 프로세스에서 추가 할 수 있고 다른 프로세스에서 읽을 수 있다는 가정하에있었습니다. 큐가 전달되고 부모 큐는 큐에 추가되지 않는 것처럼 보입니다. 부모가 만든 두 개의 다른 프로세스에서 어떤 유형의 대기열에 항목을 추가 할 수 있습니까?

from multiprocessing import Process, Queue 
import time 


def f(q): 

    for a in range(5): 
     print "Adding to Q!" 
     time.sleep(1) 
     q.put(a) 

def g(q): 

    i = 0 
    v = True 
    q.put("Adding to this q") 
    while v == True: 
     time.sleep(1) 
     i = i + 1 
     print "Get slept time " , i 
     try: 
       print "From the q " + str(q.get(True,1)) 
     except: 
       print "Empty" 
     if i == 10: 
       v = False 

if __name__ == '__main__': 
    q = Queue(10) 
    print "First process" 
    p = Process(target=f, args=(q,)) 
    p.start() 

    print "Second Process" 
    p1 = Process(target=g, args=(q,)) 
    p1.start() 

    p.join() 
    p1.join() 

추가 또는이) (가입 제거는 더는

내 출력 지금과 같은 원하는 출력에 영향이없는 것으로 보인다. **이 원하는 출력 **

First process 
Second Process 
Adding to Q! 
Get slept time 1 
From the q Adding to this q 
Adding to Q! 
Get slept time 2 
Adding to Q! 
Empty 
Get slept time 3 
Adding to Q! 
Empty 
Get slept time 4 
Adding to Q! 
Empty 
Get slept time 5 
Empty 
Get slept time 6 
Empty 
Get slept time 7 
Empty 
Get slept time 8 
Empty 
Get slept time 9 
Empty 
Get slept time 10 
Empty 
+0

python 2.6 x64를 사용하여 centos 6.5에서 테스트했으며 원하는 결과를 얻었습니다. –

+0

내가 가지고있는 출력을 얻었습니까? 아니면 다른 결과를 얻었습니까? 게시 할 수 있습니까? – tiggles

답변

1

가 아니라, 문제가 큐 아닙니다에서 그것의
없는 당신의 print "From the q " + q.get(True,1)

당신이 더 print "From the q {}".format(q.get(True,1))

를 사용하여 예외 TypeError: cannot concatenate 'str' and 'int' objects

입니다

@tiggles 다음에 편집 :
대기열에서 읽거나 쓰는 데 문제가 없습니다. ue.

from multiprocessing import Process, Queue 
import time 

def f(q): 

    for a in range(5):  
     time.sleep(1) 
     q.put(a) 

def g(q): 

    i = 0 
    v = True 
    q.put("Adding to this q") 
    while v == True: 
     time.sleep(1) 
     i = i + 1 
     print "Get slept time " , i 
     try: 
      print "From the q {}".format(q.get(True,1)) 
     except Exception as e: 
      print 'exception raised {} {}'.format(e, type(e)) 
      print "Empty" 
     if i == 10: 
      v = False 

if __name__ == '__main__': 
    q = Queue(10) 
    p = Process(target=f, args=(q,)) 
    p.start() 

    p1 = Process(target=g, args=(q,)) 
    p1.start() 

    p.join() 
    p1.join() 
    print q.qsize(), q.empty() 

을하고 결과 :

Get slept time 1 
From the q Adding to this q 
Get slept time 2 
From the q 0 
Get slept time 3 
From the q 1 
Get slept time 4 
From the q 2 
Get slept time 5 
From the q 3 
Get slept time 6 
From the q 4 
Get slept time 7 
exception raised <class 'Queue.Empty'> 
Empty 
Get slept time 8 
exception raised <class 'Queue.Empty'> 
Empty 
Get slept time 9 
exception raised <class 'Queue.Empty'> 
Empty 
Get slept time 10 
exception raised <class 'Queue.Empty'> 
Empty 
0 True 

질문이 여기
는 작동하는 코드 예제입니다?

+0

그래, 나는 그것을 문자열로 던져 넣었지만 그 코드를 복사하지 않았고 내가 던지기를 놓친 것 같습니다. 하지만 심지어 그 문제를 해결하면 왜 내가 첫 번째 스레드에 의해 대기열에서 값을 읽을 수 없는지 수정하지 않습니다 – tiggles

+0

@tiggles 내 변경 사항을 눈치 챘나요? – NoamG

+0

예, 이제 알았습니다. .format (q.get (True, 1))과 함께 잘 작동합니다. 왜 예외 출력을 인쇄하지 않았기 때문에 내가 얻은 오류가 보이지 않는지 이해합니다. 나는 단지 "Empty"를 인쇄하고 있었다. 도와 줘서 고마워 – tiggles

관련 문제