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

swh:1:snp:9023cfe7eac951feac42e3bd4133ce866a0fe0f0
  • Code
  • Branches (1)
  • Releases (0)
    • Branches
    • Releases
    • HEAD
    • refs/heads/master
    No releases to show
  • 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
  • revision
  • snapshot
content badge Iframe embedding
swh:1:cnt:daac999214b851ffe850db27d1b40b5394f1d9b3
directory badge Iframe embedding
swh:1:dir:2837cecedb2ca8992d5afa9ee54102a1e8fa61b6
revision badge
swh:1:rev:f08bd75aabf7213e253baf26d219c374a745c8d4
snapshot badge
swh:1:snp:9023cfe7eac951feac42e3bd4133ce866a0fe0f0
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
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: f08bd75aabf7213e253baf26d219c374a745c8d4 authored by Christopher McFarland on 04 February 2019, 22:56:03 UTC
refract
Tip revision: f08bd75
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