मैं मल्टीप्रोसेसिंग के साथ प्रयोग कर रहा हूं, और डेमन्स के साथ दिमाग में चल रहा हूं।पाइथन मल्टीप्रोसेसिंग डिमन प्रक्रिया मानक आउटपुट पर प्रिंट क्यों नहीं कर रही है?
मेरे पास एक डिमन और एक गैर-डिमन प्रक्रिया है, डेमॉन उत्सर्जन आउटपुट प्रत्येक एक अनिश्चित काल तक, जबकि गैर-डिमन प्रिंट शुरू होने पर तुरंत आउटपुट करता है, फिर 3 सेकंड तक सो जाता है, फिर प्रिंट करता है और लौटाता है।
समस्या यह है कि डेमॉन प्रक्रिया से अपेक्षित आउटपुट बिल्कुल दिखाई नहीं देता है।
डेमन्स पर पिछले SO प्रश्नों की समीक्षा करना, आम समस्याएं या तो डेमॉन से पहले समाप्त होने वाली अन्य प्रक्रियाएं होती हैं, या स्टडआउट को आउटपुट दिखाने के लिए फ़्लशिंग की आवश्यकता होती है। दोनों में (I लगता है) संबोधित किया गया है, हालांकि मैं केवल गैर-डेमोनिक प्रक्रियाओं से मुद्रित आउटपुट देखना जारी रखता हूं।
कोड:
from multiprocessing import Process, current_process
import sys
import time
def worker():
"""
Announce that the process has started, sleep 3 seconds
then announce that the process is ending.
"""
name = current_process().name
print name, 'starting...'
sys.stdout.flush()
time.sleep(3)
print name, 'ending...'
sys.stdout.flush()
return
def daemon():
"""
Announce that the process has started, beep, then beep
once every second
"""
name = current_process().name
print name, 'starting...'
print 'beep...'
sys.stdout.flush()
while True:
time.sleep(1)
print 'beep...'
sys.stdout.flush()
if __name__=='__main__':
d = Process(target=daemon)
d.daemon = True
d.start()
p = Process(target=worker)
p.daemon = False
p.start()
अपेक्षित आउटपुट:
Process-1 starting... # Order here may vary
beep...
Process-2 starting...
beep...
beep...
Process-2 ending... #There may or may not be another beep here
क्या वास्तव में उत्पादन किया जाता है:
Process-2 starting...
Process-2 ending...
यह क्यों हो रहा है पर कोई सलाह वास्तव में सराहना की जाएगी।
आह मैं देखता हूं, मुख्य प्रक्रिया समाप्त होने पर डिमन समाप्त हो जाता है, न कि जब सभी गैर-डिमन बच्चे प्रक्रिया समाप्त होती है। समझ में आता है, बहुत धन्यवाद! मुझे लगता है कि मुख्य प्रक्रिया के अंत में एक p.join() जोड़ने से यह गैर-डिमन समाप्त होने की प्रतीक्षा करेगा। – zehnpaard