<commit_before>
import sys
import time
import threading
import itertools

zip = getattr(itertools, 'izip', zip)
#from itertools import izip, count

#http://wiki.scipy.org/Cookbook/Multithreading
#https://github.com/scipy/scipy-cookbook/blob/master/ipython/attachments/Multithreading/handythread.py

def foreach(f,l,threads=3,return_=False):
    """
    Apply f to each element of l, in parallel
    """

    if threads>1:
        iteratorlock = threading.Lock()
        exceptions = []
        if return_:
            n = 0
            d = {}
            i = zip(itertools.count(),l.__iter__())
        else:
            i = l.__iter__()


        def runall():
            while True:
                iteratorlock.acquire()
                try:
                    try:
                        if exceptions:
                            return
                        v = next(i)#.next()
                    finally:
                        iteratorlock.release()
                except StopIteration:
                    return
                try:
                    if return_:
                        n,x = v
                        d[n] = f(x)
                    else:
                        f(v)
                except:
                    e = sys.exc_info()
                    iteratorlock.acquire()
                    try:
                        print(e)
                        exceptions.append(e)
                    finally:
                        iteratorlock.release()
        
        threadlist = [threading.Thread(target=runall) for j in range(threads)]
        for t in threadlist:
            t.start()
        for t in threadlist:
            t.join()
        if exceptions:
            a, b, c = exceptions[0]
            raise(a, b, c)
        if return_:
            r = sorted(d.items())
#            r.sort()
            return [v for (n,v) in r]<commit_msg>Undid change in handythread<commit_after>
import sys
import time
import threading
import itertools

zip = getattr(itertools, 'izip', zip)
#from itertools import izip, count

#http://wiki.scipy.org/Cookbook/Multithreading
#https://github.com/scipy/scipy-cookbook/blob/master/ipython/attachments/Multithreading/handythread.py

def foreach(f,l,threads=3,return_=False):
    """
    Apply f to each element of l, in parallel
    """

    if threads>1:
        iteratorlock = threading.Lock()
        exceptions = []
        if return_:
            n = 0
            d = {}
            i = zip(itertools.count(),l.__iter__())
        else:
            i = l.__iter__()


        def runall():
            while True:
                iteratorlock.acquire()
                try:
                    try:
                        if exceptions:
                            return
                        v = next(i)#.next()
                    finally:
                        iteratorlock.release()
                except StopIteration:
                    return
                try:
                    if return_:
                    	n,x = v
                        d[n] = f(x)
                    else:
                        f(v)
                except:
                    e = sys.exc_info()
                    iteratorlock.acquire()
                    try:
                        print(e)
                        exceptions.append(e)
                    finally:
                        iteratorlock.release()
        
        threadlist = [threading.Thread(target=runall) for j in range(threads)]
        for t in threadlist:
            t.start()
        for t in threadlist:
            t.join()
        if exceptions:
            a, b, c = exceptions[0]
            raise(a, b, c)
        if return_:
            r = sorted(d.items())
#            r.sort()
            return [v for (n,v) in r]
{'fraction_non_alphanumeric': 0.09174795676245716, 'fraction_numerical': 0.0021091484313208544, 'mean_word_length': 1.0430802369413033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608889', 'n_tokens_mistral': 1034, 'n_tokens_neox': 981, 'n_words': 236}