| wikis | index


Multiprocessing

fork:

import os
newpid = os.fork()
if newpid == 0:
    child()
else:
    parent()
print(os.getpid())

https://serverfault.com/a/598005

https://docs.python.org/3/library/multiprocessing.html

Threading

https://docs.python.org/3/library/threading.html

  • Daemon thread - entire program exists only when daemon threads are left.
  • join() waits until the thread terminates
  • Lock
    • locked/unlocked (default state)
    • acquire(): set locked=True and return if unlocked; otherwise block
    • release(): set locked=False
  • Rlock Reentrant lock. May be acquired several times by the same thread. It introduces the concept of "owning thread" and "recursion level". Only after final release() the lock is released.
  • Condition - for consumer/producer
    • wait()
    • notify(), notify_all()
  • Semaphore - one of the oldest sync primitives, introduced by Dijkstra. Guards resources with limited capacity. E.g. connection pool.
    • P() -> acquire(): counter--; block if counter == 0
    • V() -> release(): counter++
  • Event simple mechanism. set()/clear(), wait()
  • Timer - call action after a certain time
  • Barrier - block until a given threshold has been reached. Then release all threads simultaneously.

A Lock, Condition and a Semaphores (all w. acquire() and release()) can be used in the with statement.