from neuron import h
import logging

_neuron_parallel_log = logging.getLogger('allensdk.model.biophysical.neuron_parallel')

_pc = h.ParallelContext()

def map(func, *iterables):
    start_time = pc_time()
    userids = []
    userid = 200 # arbitrary, but needs to be a positive integer
    for args in zip(*iterables):
        args2 = (list(a) for a in args)
        _pc.submit(userid, func, *args2)
        userids.append(userid)
        userid += 1
    results = dict(working())
    end_time = pc_time()
    _neuron_parallel_log.debug("Map took %s" % (str(end_time - start_time)))
    return [results[userid] for userid in userids]

def working():
    while _pc.working():
        userid = int(_pc.userid())
        ret = _pc.pyret()
        yield userid, ret

def runworker():
    _pc.runworker()

def done():
    _pc.done()

def pc_time():
    return _pc.time()

def reset_neuron_library():
    '''
    See Also: https://www.neuron.yale.edu/phpBB/viewtopic.php?f=2&t=2367
    '''
    _pc.gid_clear()
    
    for sec in h.allsec():
        h("%s{delete_section()}" % (sec.name()) )
    
    