Reference

multiprocessing, Threading, and Thread

multiprocessing

Basic usage

def func(value, comment):
   .......
   .......
def func1((value, comment)):
   .......
   .......

multiprocessing.Process

import multiprocessing

val = ...
com = ...
p = multiprocessing.Process(target=func, args=(val, com))
p.daemon = True
p.start()
p.join()

The multiprocessing module allows the programmer to fully leverage multiple processors on a given machine.

multiprocessing.Pool

def func1((value, comment)):
   .......
   .......
import multiprocessing

pool = multiprocessing.Pool(processes=9)
result = pool.map(func1, [(1, 'hoge'), (2, 'huga'), (3, 'huga'), (4, 'huga'), (5, 'huga'), (6, 'huga'), (7, 'huga'), (8, 'huga'), (9, 'huga'),])

Threading

import threading

val = ...
com = ...
t = threading.Thread(target=func, args=(val, com))
t.daemon = True
t.start()
t.join()

This module constructs higher-level threading interfaces on top of the lower level thread module.

Thread

import thread

val = ...
com = ...
t = thread.start_new_thread(target=func, args=(val, com))

This module provides low-level primitives for working with multiple threads (also called light-weight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higher-level threading API built on top of this module.