If the work you want to do with elements from the permutation generator has processor intensity, you probably want to use processes, not threads. CPython Global Interpreter Lock (GIL) makes multithreading of limited value when working with the CPU.
Instead, use the multiprocessing module Pool , for example:
import multiprocessing import itertools def do_stuff(perm):
Please note that if you iterate over the results (instead of doing something with it as a list), you can use imap instead of map to get an iterator that you can use to work with the results since they are produced from work processes. If it doesn't matter in which order the elements are returned, you can use imap_unordered to (I think) save some memory.
The if __name__ is "__main__" template if __name__ is "__main__" required on Windows, where the multiprocessing module must work with OS restrictions (no fork ).
source share