Commit 19cf329f authored by David Hendriks's avatar David Hendriks
Browse files

added random multiprocessing examples

parent 3be4dd95
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
import os, sys
import matplotlib.pyplot as plt

import binary_c

# Append root dir of this project to include functionality
sys.path.append(os.path.dirname(os.getcwd()))
import binary_c

from utils.defaults import physics_defaults
from utils.functions import create_arg_string

+31 −0
Original line number Diff line number Diff line
#!/usr/bin/python3
import os
import sys

# Append root dir of this project to include functionality
sys.path.append(os.path.dirname(os.getcwd()))
import binary_c

from utils.defaults import physics_defaults
from utils.functions import create_arg_string


def run_test_binary():
    m1 = 15.0 # Msun
    m2 = 14.0 # Msun
    separation = 0 # 0 = ignored, use period
    orbital_period = 4530.0 # days
    eccentricity = 0.0
    metallicity = 0.02
    max_evolution_time = 15000
    buffer = ""
    # argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g}  ".format(m1,m2,separation,orbital_period,eccentricity,metallicity,max_evolution_time)
    
    argstring = f"binary_c M_1 {m1} M_2 {m2} separation {separation} orbital_period {orbital_period} eccentricity {eccentricity} metallicity {metallicity} max_evolution_time {max_evolution_time}"

    output = binary_c.run_binary(argstring)

    # print ("Binary_c output:\n\n")
    print (output)

run_test_binary()

snippets/d.py

0 → 100644
+25 −0
Original line number Diff line number Diff line
import multiprocessing
import time

 
def doubler(number):
    return number ** 2
 
def count(number):
    nr = 0
    for i in range(number):
        nr += i
    return number

if __name__ == '__main__':
    numbers = range(2, 100000)
    pool = multiprocessing.Pool(processes=6)
    # print()

    rs = pool.map_async(pool.map(count, numbers), range(len(numbers)))
    pool.close() # No more work
    while (True):
      if (rs.ready()): break
      remaining = rs._number_left
      print("Waiting for", remaining, "tasks to complete...")
      time.sleep(0.5)
 No newline at end of file

snippets/mp2.py

0 → 100644
+17 −0
Original line number Diff line number Diff line
import time

def basic_func(x):
    if x == 0:
        return 'zero'
    elif x%2 == 0:
        return 'even'
    else:
        return 'odd'
    
starttime = time.time()
for i in range(0,10):
    y = i*i
    time.sleep(2)
    print('{} squared results in a/an {} number'.format(i, basic_func(y)))
    
print('That took {} seconds'.format(time.time() - starttime))
 No newline at end of file

snippets/mp3.py

0 → 100644
+28 −0
Original line number Diff line number Diff line
import time
import multiprocessing 

def basic_func(x):
    if x == 0:
        return 'zero'
    elif x%2 == 0:
        return 'even'
    else:
        return 'odd'

def multiprocessing_func(x):
    y = x*x
    time.sleep(2)
    print('{} squared results in a/an {} number'.format(x, basic_func(y)))
    
if __name__ == '__main__':
    starttime = time.time()
    processes = []
    for i in range(0,100):
        p = multiprocessing.Process(target=multiprocessing_func, args=(i,))
        processes.append(p)
        p.start()
        
    for process in processes:
        process.join()
        
    print('That took {} seconds'.format(time.time() - starttime))
 No newline at end of file
Loading