1"""
2PySCeS - Python Simulator for Cellular Systems (http://pysces.sourceforge.net)
3
4Copyright (C) 2004-2020 B.G. Olivier, J.M. Rohwer, J.-H.S Hofmeyr all rights reserved,
5
6Johann Rohwer (jrohwer@users.sourceforge.net)
7Triple-J Group for Molecular Cell Physiology
8Stellenbosch University, South Africa.
9
10Permission to use, modify, and distribute this software is given under the
11terms of the PySceS (BSD style) license. See LICENSE.txt that came with
12this distribution for specifics.
13
14NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
15Johann M. Rohwer
16"""
17from __future__ import division, print_function
18from __future__ import absolute_import
19from __future__ import unicode_literals
20
21import sys, pickle
22import multiprocessing
23from pysces.PyscesParScan import Analyze, setModValue
24from pysces.PyscesUtils import TimerBox
25from time import sleep
26
27
28if __name__ == '__main__':
29    print("\nmultiprocessing script command line:")
30    print(sys.argv)
31    # called with
32    # subprocess.call(['python', MULTISCANFILE, self._MODE_, fN])
33    MODE = sys.argv[1]
34    pool = multiprocessing.Pool()
35
36    # load stuff from the pickle
37    F = open(sys.argv[2],'rb')
38    mod, scanpartition, seqpartition, genorder, useroutputlist = pickle.load(F)
39    F.close()
40
41    mod.SetQuiet()  # kill verbose output during scan
42    # append tasks to asynchronous results list
43    arl = []
44    for i in range(len(scanpartition)):
45        arl.append(pool.apply_async(Analyze, (scanpartition[i], seqpartition[i], genorder, MODE, useroutputlist, mod)))
46
47    print("Submitted tasks:", len(arl))
48    print("\nPARALLEL COMPUTATION\n--------------------")
49
50    while arl[-1].ready() != True:
51        sleep(5)
52        print('Tasks to go... ', [r.ready() for r in arl].count(False))
53        # wait until all tasks are completed
54    arl[-1].wait()
55
56    # get results
57    print("\nGATHER RESULT\n-------------")
58    res_list = []
59    for ar in arl:
60        res_list.append(ar.get())
61    # pickle results_list
62    F = open(sys.argv[2], 'wb')
63    pickle.dump(res_list, F, protocol=-1)
64    F.flush()
65    F.close()
66