Answers for "python multiprocessing"

4

multiprocessing python

"""A very simple parallel code example to execute parallel functions in python"""
import multiprocessing
import numpy as np
def multiprocessing_func(x):
	"""Individually prints the squares y_i of the elements x_i of a vector x"""
	for x_i in x:
		y=x_i**2 
		print('The square of ',x_i,' is ',y)
def chunks(input, n):
    """Yields successive n-sized chunks of input"""
    for i in range(0, len(input), n):
        yield input[i:i + n]
if __name__=='__main__':
	n_proc=4 #Numer of available processors
	x=np.arange(100) #Input
	chunked_x=list(chunks(x, int(x.shape[0]/n_proc)+1)) #Splits input among n_proc chunks
	processes=[] #Initialize the parallel processes list
	for i in np.arange(0,n_proc):
		"""Execute the target function on the n_proc target processors using the splitted input""" 
		p = multiprocessing.Process(target=multiprocessing_func,args=(chunked_x[i],))
		processes.append(p)
		p.start()
	for process in processes:
		process.join()
Posted by: Guest on January-26-2021
1

multiprocessing join python

from multiprocessing import Process

def say_hello(name='world'):
    print "Hello, %s" % name

p = Process(target=say_hello)
p.start()
p.join()	# Tells the program to wait until p has finished it's job before exiting
Posted by: Guest on November-26-2020
1

python - multiprocessing

import time
from multiprocessing import Process

# My functions (threads)
def my_func_1():...
def my_func_2():...

# Single calculation  
start = time.time()
my_func_1()
my_func_2()
print(f'Single thread total time: {time.time() - start}')

# Processes
process = Process(target=my_func_1)
process2 = Process(target=my_func_2)
process.start()
process2.start()

start = time.time() # Start the two processes

process.join()      # Wait till processes finish
process2.join()

print(f'Two thread total time: {time.time() - start}')
Posted by: Guest on September-14-2020
1

python multiprocessing

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))
Posted by: Guest on March-16-2020

Code answers related to "python multiprocessing"

Python Answers by Framework

Browse Popular Code Answers by Language