Skip to content

Instantly share code, notes, and snippets.

@tbelahi
Last active November 22, 2016 10:31
Show Gist options
  • Save tbelahi/c93840108885253c18c35b10ab179aeb to your computer and use it in GitHub Desktop.
Save tbelahi/c93840108885253c18c35b10ab179aeb to your computer and use it in GitHub Desktop.
#!/bin/env python
# coding: utf-8
from multiprocessing import Pool
import time
import subprocess as sp
def print_names(liste_noms):
for nom in liste_noms:
time.sleep(1)
# boucle qui sert à "occuper" le cpu
c = 0
n = 5*10**6
for i in range(n):
c += i
print("mon nom est:{}, somme des {} premiers entiers: {}".format(nom, n, c))
def echo_name(nom):
#return sp.call(["echo", nom], shell=True)
time.sleep(1)
# boucle qui sert à "occuper" le cpu
c = 0
n = 5*10**6
for i in range(n):
c += i
return sp.call("echo '{}, {}, {}'".format(nom, n, c), shell=True)
def main():
pool = Pool(processes=2)
noms_a = ["anna", "andré", "arnaud", "arnold", "anselme", "adeline", "alain"]
noms_e = ["eric", "eddy", "eli", "emelyne", "edithe", "edmond", "edouard"]
# go open "htop"
time.sleep(5)
# launch processes
print("\nPremière approche\n")
pool.map(print_names, [noms_a, noms_e])
print("\nSeconde approche\n")
pool.map(echo_name, noms_a + noms_e)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment