शर्मनाक समानांतर समस्याओं से निपटने के लिए मल्टीप्रोसेसिंग का उपयोग कैसे किया जाता है ?
आमतौर पर समानांतर समस्याओं में तीन मूल भाग होते हैं:
- इनपुट डेटा (एक फ़ाइल, डेटाबेस, टीसीपी कनेक्शन आदि से) पढ़ें ।
- इनपुट डेटा पर गणना चलाएं , जहां प्रत्येक गणना किसी अन्य गणना से स्वतंत्र है ।
- गणना के परिणाम लिखें (एक फ़ाइल, डेटाबेस, टीसीपी कनेक्शन, आदि के लिए)।
हम दो आयामों में कार्यक्रम को समानांतर कर सकते हैं:
- भाग 2 कई कोर पर चल सकता है, क्योंकि प्रत्येक गणना स्वतंत्र है; प्रसंस्करण का क्रम मायने नहीं रखता।
- प्रत्येक भाग स्वतंत्र रूप से चल सकता है। भाग 1 एक इनपुट कतार पर डेटा रख सकता है, भाग 2 इनपुट कतार से डेटा खींच सकता है और एक आउटपुट कतार पर परिणाम डाल सकता है, और भाग 3 आउटपुट कतार से परिणाम खींच सकता है और उन्हें लिख सकता है।
यह समवर्ती प्रोग्रामिंग में एक सबसे बुनियादी पैटर्न लगता है, लेकिन मैं अभी भी इसे हल करने की कोशिश में खो गया हूं, तो आइए एक उदाहरण के रूप में यह वर्णन करने के लिए कि यह मल्टीप्रोसेसिंग का उपयोग करके कैसे किया जाता है ।
यहाँ उदाहरण की समस्या है: इनपुट के रूप में पूर्णांक की पंक्तियों के साथ एक सीएसवी फ़ाइल को देखते हुए , उनकी रकम की गणना करें। समस्या को तीन भागों में अलग करें, जो सभी समानांतर में चल सकते हैं:
- इनपुट फ़ाइल को कच्चे डेटा में संसाधित करें (पूर्णांकों की सूची / पुनरावृत्तियों)
- समानांतर में, डेटा के योगों की गणना करें
- रकम का उत्पादन
नीचे पारंपरिक, एकल-प्रक्रिया बाध्य पायथन कार्यक्रम है जो इन तीन कार्यों को हल करता है:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# basicsums.py
"""A program that reads integer values from a CSV file and writes out their
sums to another CSV file.
"""
import csv
import optparse
import sys
def make_cli_parser():
"""Make the command line interface parser."""
usage = "\n\n".join(["python %prog INPUT_CSV OUTPUT_CSV",
__doc__,
"""
ARGUMENTS:
INPUT_CSV: an input CSV file with rows of numbers
OUTPUT_CSV: an output file that will contain the sums\
"""])
cli_parser = optparse.OptionParser(usage)
return cli_parser
def parse_input_csv(csvfile):
"""Parses the input CSV and yields tuples with the index of the row
as the first element, and the integers of the row as the second
element.
The index is zero-index based.
:Parameters:
- `csvfile`: a `csv.reader` instance
"""
for i, row in enumerate(csvfile):
row = [int(entry) for entry in row]
yield i, row
def sum_rows(rows):
"""Yields a tuple with the index of each input list of integers
as the first element, and the sum of the list of integers as the
second element.
The index is zero-index based.
:Parameters:
- `rows`: an iterable of tuples, with the index of the original row
as the first element, and a list of integers as the second element
"""
for i, row in rows:
yield i, sum(row)
def write_results(csvfile, results):
"""Writes a series of results to an outfile, where the first column
is the index of the original row of data, and the second column is
the result of the calculation.
The index is zero-index based.
:Parameters:
- `csvfile`: a `csv.writer` instance to which to write results
- `results`: an iterable of tuples, with the index (zero-based) of
the original row as the first element, and the calculated result
from that row as the second element
"""
for result_row in results:
csvfile.writerow(result_row)
def main(argv):
cli_parser = make_cli_parser()
opts, args = cli_parser.parse_args(argv)
if len(args) != 2:
cli_parser.error("Please provide an input file and output file.")
infile = open(args[0])
in_csvfile = csv.reader(infile)
outfile = open(args[1], 'w')
out_csvfile = csv.writer(outfile)
# gets an iterable of rows that's not yet evaluated
input_rows = parse_input_csv(in_csvfile)
# sends the rows iterable to sum_rows() for results iterable, but
# still not evaluated
result_rows = sum_rows(input_rows)
# finally evaluation takes place as a chain in write_results()
write_results(out_csvfile, result_rows)
infile.close()
outfile.close()
if __name__ == '__main__':
main(sys.argv[1:])
चलो इस कार्यक्रम को लेते हैं और इसे ऊपर उल्लिखित तीन हिस्सों को समानांतर करने के लिए मल्टीप्रोसेसिंग का उपयोग करने के लिए फिर से लिखते हैं। नीचे इस नए, समानांतर कार्यक्रम का एक कंकाल है, जिसे टिप्पणियों में भागों को संबोधित करने के लिए दूर करने की आवश्यकता है:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# multiproc_sums.py
"""A program that reads integer values from a CSV file and writes out their
sums to another CSV file, using multiple processes if desired.
"""
import csv
import multiprocessing
import optparse
import sys
NUM_PROCS = multiprocessing.cpu_count()
def make_cli_parser():
"""Make the command line interface parser."""
usage = "\n\n".join(["python %prog INPUT_CSV OUTPUT_CSV",
__doc__,
"""
ARGUMENTS:
INPUT_CSV: an input CSV file with rows of numbers
OUTPUT_CSV: an output file that will contain the sums\
"""])
cli_parser = optparse.OptionParser(usage)
cli_parser.add_option('-n', '--numprocs', type='int',
default=NUM_PROCS,
help="Number of processes to launch [DEFAULT: %default]")
return cli_parser
def main(argv):
cli_parser = make_cli_parser()
opts, args = cli_parser.parse_args(argv)
if len(args) != 2:
cli_parser.error("Please provide an input file and output file.")
infile = open(args[0])
in_csvfile = csv.reader(infile)
outfile = open(args[1], 'w')
out_csvfile = csv.writer(outfile)
# Parse the input file and add the parsed data to a queue for
# processing, possibly chunking to decrease communication between
# processes.
# Process the parsed data as soon as any (chunks) appear on the
# queue, using as many processes as allotted by the user
# (opts.numprocs); place results on a queue for output.
#
# Terminate processes when the parser stops putting data in the
# input queue.
# Write the results to disk as soon as they appear on the output
# queue.
# Ensure all child processes have terminated.
# Clean up files.
infile.close()
outfile.close()
if __name__ == '__main__':
main(sys.argv[1:])
कोड के इन टुकड़ों के साथ-साथ कोड का एक और टुकड़ा जो परीक्षण उद्देश्यों के लिए उदाहरण CSV फ़ाइलों को उत्पन्न कर सकता है, जीथब पर पाया जा सकता है ।
मैं यहां किसी भी अंतर्दृष्टि की सराहना करता हूं कि आप इस समस्या को कैसे समझेंगे।
इस समस्या के बारे में सोचते समय मेरे पास कुछ प्रश्न हैं। किसी भी / सभी को संबोधित करने के लिए बोनस अंक:
- क्या मेरे पास डेटा में पढ़ने और इसे कतार में रखने के लिए बाल प्रक्रियाएं होनी चाहिए, या जब तक सभी इनपुट नहीं पढ़ लिए जाते हैं, तब तक मुख्य प्रक्रिया इसे अवरुद्ध किए बिना कर सकती है?
- इसी तरह, क्या मेरे पास संसाधित कतार से परिणाम लिखने के लिए एक बच्चे की प्रक्रिया होनी चाहिए, या सभी परिणामों की प्रतीक्षा किए बिना मुख्य प्रक्रिया ऐसा कर सकती है?
- क्या मुझे योग कार्यों के लिए एक प्रक्रिया पूल का उपयोग करना चाहिए ?
- यदि हाँ, तो इनपुट और आउटपुट प्रक्रियाओं को अवरुद्ध किए बिना, इनपुट कतार में आने वाले परिणामों को संसाधित करने के लिए इसे प्राप्त करने के लिए मैं पूल पर किस विधि से कॉल करूं? apply_async () ? map_async () ? imap () ? imap_unordered () ?
- मान लीजिए कि हमें इनपुट और आउटपुट कतारों से बाहर निकलने की जरूरत नहीं है क्योंकि डेटा ने उन्हें दर्ज किया था, लेकिन जब तक सभी इनपुट को पार्स नहीं किया गया था तब तक इंतजार कर सकता था और सभी परिणामों की गणना की गई थी (उदाहरण के लिए, क्योंकि हम जानते हैं कि सभी इनपुट और आउटपुट सिस्टम मेमोरी में फिट होंगे)। क्या हमें एल्गोरिथ्म को किसी भी तरह से बदलना चाहिए (जैसे, I / O के साथ समवर्ती कोई प्रक्रिया नहीं चलाना)?