Explore tens of thousands of sets crafted by our community.
Parallel Computing with Python and multiprocessing
25
Flashcards
0/25
timeout argument in methods
Many multiprocessing objects accept a 'timeout' parameter, allowing operations to wait for a specified time before giving up. Example Use: q.get(timeout=5)
Pool class
Offers a convenient means of parallelizing the execution of a function across multiple input values. Example Use: with multiprocessing.Pool(processes=4) as pool: results = pool.map(function, iterable)
active_children function
Returns a list of all live children of the current process. Example Use: children = multiprocessing.active_children()
Condition class
A synchronization object that allows one or more processes to wait until notified by another process. Example Use: cond = multiprocessing.Condition()
Manager class
Used for controlling a server process that holds Python objects and allows other processes to manipulate them. Example Use: with multiprocessing.Manager() as manager: l = manager.list()
Barrier class
A synchronization object for indicating that a certain number of processes have reached a particular point in their execution. Example Use: barrier = multiprocessing.Barrier(5)
starmap method in Pool class
Similar to map, but the elements of the iterable are expected to be iterables that are unpacked as arguments. Example Use: results = pool.starmap(function, [(arg1, arg2), (arg3, arg4)])
multiprocessing module
A Python module that supports spawning processes using an API similar to the threading module. Example Use: import multiprocessing as mp
Semaphore class
A synchronization object that implements a semaphore for limiting access to a shared resource. Example Use: sem = multiprocessing.Semaphore(2)
Value class
Used for creating a ctypes object in shared memory that can be accessed by different processes. Example Use: val = multiprocessing.Value('i', 0)
SimpleQueue class
Provides a simple FIFO queue that is especially useful for communicating between processes. Example Use: sq = multiprocessing.SimpleQueue()
Queue class
A queue, especially useful for processes as it is a safe way to pass messages between processes. Example Use: q = multiprocessing.Queue()
Lock class
A lock object that is used to synchronize processes, preventing simultaneous access to a shared resource. Example Use: lock = multiprocessing.Lock()
Array class
Used to create an array of ctypes in shared memory, which can be accessed by different processes. Example Use: arr = multiprocessing.Array('i', [1, 2, 3, 4])
current_process function
Returns the Process object corresponding to the current process. Example Use: current = multiprocessing.current_process()
cpu_count function
Returns the number of CPU cores that are available. Example Use: num_cores = multiprocessing.cpu_count()
JoinableQueue class
A Queue that additionally has join() and task_done() methods, allowing processes to wait for queue's tasks to be completed. Example Use: jq = multiprocessing.JoinableQueue()
finalize method in Pool class
Closes the pool and terminates its worker processes after completing all pending work. Example Use: pool.close(); pool.join();
Event class
A synchronization object that is used to signal between processes. Typically used to indicate a condition or a change. Example Use: event = multiprocessing.Event()
Pipe function
Returns a pair of Connection objects representing the ends of a pipe. Example Use: parent_conn, child_conn = multiprocessing.Pipe()
RLock class
A reentrant lock object that can be acquired multiple times, by the same process. Example Use: rlock = multiprocessing.RLock()
initializer and initargs arguments in Pool constructor
Used to specify an initialization function and arguments for each worker process when creating a Pool. Example Use: pool = multiprocessing.Pool(initializer=init_func, initargs=(arg1,))
imap and imap_unordered methods in Pool class
Variants of map method that return an iterator over the results in order (imap) or in any order as they complete (imap_unordered). Example Use: for result in pool.imap_unordered(function, iterable): process(result)
Process class
Represents an activity that is run in a separate process. Example Use: p = multiprocessing.Process(target=function, args=(arg1,))
apply_async method in Pool class
Applies a function asynchronously using arguments, returning a result object. Example Use: result = pool.apply_async(function, (arg1,))
© Hypatia.Tech. 2024 All rights reserved.