Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'processed': 0.05; 'python3': 0.05; '__name__': 0.07; 'main()': 0.07; 'next,': 0.07; 'subject:How': 0.09; 'filename,': 0.09; 'files:': 0.09; 'other)': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'sep': 0.09; 'def': 0.10; 'files.': 0.13; "skip:' 30": 0.15; "'__main__':": 0.16; "'a',": 0.16; "'b',": 0.16; "'c'": 0.16; 'dhananjay': 0.16; 'files)': 0.16; 'functions;': 0.16; 'main():': 0.16; 'true:': 0.16; 'url:py3k': 0.16; 'wrote:': 0.17; 'skip:{ 20': 0.17; 'input': 0.18; 'received:209.85.212.46': 0.18; 'sender:addr:gmail.com': 0.18; 'all,': 0.21; 'trying': 0.21; 'import': 0.21; 'file:': 0.22; 'seems': 0.23; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'first,': 0.27; 'skip:# 10': 0.27; 'core': 0.27; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'run': 0.28; 'queue': 0.29; 'class': 0.29; 'function': 0.30; 'implement': 0.32; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'follows:': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'execute': 0.37; 'item': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'back': 0.62; 'within': 0.64; 'other.': 0.64; 'want,': 0.65; 'dear': 0.66; 'pools': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:content-type; bh=s5NBHOr5+cLBht2Rmil4I4iMAj9ZgWLC4f5G5nwkEUQ=; b=ObiJ9ABCf/Hl8OET8D1f9Z4/eYVtkylvt1Ij0GM0mnLsXS3+WYMvIlR6s5I0PXnkba 7eXNR2J61aabjoHAsb01mT5LovoXeiwxfFBbvP9M2sG18kDJM7uxi91zDQbU7edWEk0T Musx20b3cmYfsu/4kRlOlZMeczWOCKDi2EABb0se77GO0Vzx3HaYpE+sHl2JMOvsr2Uj oW36ynH9ukeXQoR2kgNfPTcQrrg7PvERBqPF2b2ZEQ2zuURaj2iUPnh13F71H8H4nIIf kdT21QkqIlAERv29tnqz9Bc/CMHnxzz4ErvoJOYwLQOetD2bjEqd8225O6qqX+CDDxM6 vxaQ== MIME-Version: 1.0 Sender: kushal.kumaran@gmail.com In-Reply-To: References: From: Kushal Kumaran Date: Tue, 18 Sep 2012 14:01:55 +0530 X-Google-Sender-Auth: kYvTk2Eyt3tTlRusPZDR0iHHxBA Subject: Re: How to queue functions To: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347957139 news.xs4all.nl 6897 [2001:888:2000:d::a6]:54132 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29416 On Tue, Sep 18, 2012 at 10:26 AM, Dhananjay wrote: > Dear all, > > I am trying to use multiprocessing module. > I have 5 functions and 2000 input files. > > First, I want to make sure that these 5 functions execute one after the > other. > Is there any way that I could queue these 5 functions within the same script > ? > > > Next, as there are 2000 input files. > I could queue them by queue.put() and get back to run one by one using > queue.get() as follows: > > for file in files: > if '.dat.gz' in file: > q.put(file) > > while True: > item = q.get() > x1 = f1(item) > x2 = f2(x1) > x3 = f3(x2) > x4 = f4(x3) > final_output = f5(x4) > > > However, how can I input them on my 8 core machine, so that at a time 8 > files will be processed (to the set of 5 functions; each function one after > the other) ? > The multiprocessing.Pool class seems to be what you need. Documentation at http://docs.python.org/py3k/library/multiprocessing.html#using-a-pool-of-workers Example: #!/usr/bin/env python3 import multiprocessing def file_handler(filename): # do processing on filename, return the final_output print('working on {}'.format(filename)) return 'processed-{}'.format(filename) def main(): p = multiprocessing.Pool(8) files = [ 'a', 'b', 'c' ] result = p.map(file_handler, files) print(result) if __name__ == '__main__': main() If you want, you can also implement everything using multiprocessing.Process and multiprocessing.Queue, but using pools should be simpler. -- regards, kushal