I am not sure why you need such setup but anyway:
import _thread
import time
def operation(a, b, s):
print("Starting operation {} in process {}".format(a,b))
time.sleep(s)
print("Finished operation {} in process {}".format(a,b))
def process(n):
_thread.start_new_thread(operation, (1, n, 1))
_thread.start_new_thread(operation, (2, n, 2))
_thread.start_new_thread(operation, (3, n, 1))
_thread.start_new_thread(operation, (4, n, 2))
_thread.start_new_thread(process, (1,))
_thread.start_new_thread(process, (2,))
time.sleep(3)
Works in Python 3.4.
EDIT: As suggested, that multiprocessing may be faster, below same example with multiprocessing
from multiprocessing.pool import Pool as PoolParent
from multiprocessing import Process, Pool
import time
class NoDaemonProcess(Process):
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
class MyPool(PoolParent):
Process = NoDaemonProcess
def operation(a):
print("Starting operation {} in process {}".format(a[0],a[1]))
time.sleep(a[2])
print("Finished operation {} in process {}".format(a[0],a[1]))
def process(n):
p = Pool(4)
p.map(operation, [(1,n,1), (2,n,2), (3,n,1), (4,n,2)])
p = MyPool(2)
p.map(process, [1,2])
time.sleep(3)