import multiprocessing as mp
from time import sleep as s
print(mp.cpu_count())
def p1(queue, arg1):
print(f"Process 1 Starts with {arg1}.")
s(10)
print('Wait 5 sec. for P1.')
s(5)
print('Process 1 ends.')
queue.put(f"Result from p1 with {arg1}")
def p2(queue, arg2):
print(f'Process 2 runs with {arg2}.')
s(4)
print('In the middle of process 2.')
for i in range(10):
s(1.5)
print(i)
print("P2 is terminated.")
queue.put(f"Result from p2 with {arg2}")
if __name__ == "__main__":
queue = mp.Queue()
p1_process, p2_process = mp.Process(target=p1, args=(queue, "argument1")), mp.Process(target=p2, args=(queue, "argument2"))
p1_process.start(), p2_process.start()
p1_process.join(), p2_process.join()
result1 = queue.get()
result2 = queue.get()
print(result1)
print(result2)
More experiments can be carried out to explore what the factors are that impact execution time.
1. memory
2. data type
3. variable names (length, the complexity of identifying it)
4. the order to execute
tf.config.list_logical_devices(), tf.config.list_physical_devices()
with tf.device('gpu:0'):
tf.print(1)