Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 2837cec
  • /
  • pmap.py
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:daac999214b851ffe850db27d1b40b5394f1d9b3
directory badge Iframe embedding
swh:1:dir:2837cecedb2ca8992d5afa9ee54102a1e8fa61b6
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
pmap.py
"""Parallel (multi-threaded) map function for python. 

Uses multiprocessing.Pool with error-resistant importing. There are two map
functions:

1) pmap(function, iterable) -> rapid fork-based multi-threaded map function.

2) low_memory_pmap(function, iterable) -> a more memory-efficient version
    intended for function calls that are individually long & memory-intensive.

"""
import os
from warnings import warn
from pickle import PicklingError
import progressbar 
from time import sleep 
import multiprocessing

def fake_pmap(*args, **kwargs):
    return list(map(*args, **kwargs))

CPUs = multiprocessing.cpu_count()
CHUNKS = 50*CPUs
def pmap(func, Iter, processes=CPUs-1):
    with multiprocessing.Pool(processes=processes) as P:
        return P.map(func, Iter)

def low_memory_pmap(func, Iter, processes=int(round(CPUs/2)), chunksize=1):
    with multiprocessing.Pool(processes=processes) as P:
        return [result for result in P.imap(func, Iter)]
        
def large_iter_pmap(func, Iter, processes=CPUs-1, status_bar=True, nice=10, wait_interval=1):
    os.nice(nice)
    chunksize = max(1, int(round(len(Iter)/CHUNKS)))
    try:
        with multiprocessing.Pool(processes=processes) as P:
            rs = P.map_async(func, Iter, chunksize=chunksize)
            maxval = rs._number_left
            bar = progressbar.ProgressBar(
                maxval=maxval, 
                widgets=[progressbar.Percentage(), ' ', progressbar.Bar('=', '[', ']'), ' ', progressbar.ETA()])
            while not rs.ready():
                sleep(wait_interval)
                bar.update(maxval - rs._number_left)
            bar.finish()
            return rs.get()

    except PicklingError:
        warn("Lambda functions cannot be Pickled for Parallelization. Using single Process.", RuntimeWarning)
        return fake_pmap(func, Iter)

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top