Browse Source

Dateien hochladen nach „“

Pools
develop
raspithek 2 years ago
parent
commit
298151634d
  1. 46
      multithread.py
  2. 62
      pool.py
  3. 16
      pool2.py
  4. 20
      pool3.py
  5. 45
      race_condition_async.py

46
multithread.py

@ -0,0 +1,46 @@
from timeit import default_timer as timer
import time
import threading
def calc_square(numbers, verbose=False):
for n in range(1,numbers):
q= n*n
if verbose:
print(f'\n{n} ^ 2 = {q}')
time.sleep(0.1)
def calc_cube(numbers,verbose=False):
for n in range(1,numbers):
k = n*n*n
if verbose:
print(f'\n{n} ^ 3 = {k}')
time.sleep(0.1)
start = timer()
thread_square = threading.Thread(target=calc_square, args=(100,True))
thread_cube = threading.Thread(target=calc_cube, args=(100,True))
thread_cube.start()
thread_square.start()
thread_cube.join()
thread_square.join()
ende = timer()
differenz_mit_print = ende - start
print(f'Zeit mit print():{differenz_mit_print}s')
start = timer()
thread_square = threading.Thread(target=calc_square, args=(100,False))
thread_cube = threading.Thread(target=calc_cube, args=(100,False))
thread_cube.start()
thread_square.start()
thread_cube.join()
thread_square.join()
ende = timer()
differenz_ohne_print = ende - start
print(f'Zeit ohne print():{differenz_ohne_print}s')

62
pool.py

@ -0,0 +1,62 @@
#Quelle: https://stackoverflow.com/questions/6976372/mulitprocess-pools-with-different-functions
import datetime
import multiprocessing
import time
import random
from multiprocessing import Pool
def square(x):
# calculate the square of the value of x
print(x, x*x)
return x*x
def pf1(*args, **kwargs):
sleep_time = random.randint(3, 6)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf1", args, sleep_time, datetime.datetime.now()))
print("Keyword Args from pf1: %s" % kwargs)
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf1 done at %s\n" % datetime.datetime.now())
return (sum(*args), kwargs)
def pf2(*args):
sleep_time = random.randint(7, 10)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf2", args, sleep_time, datetime.datetime.now()))
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf2 done at %s\n" % datetime.datetime.now())
return sum(*args)
def pf3(*args):
sleep_time = random.randint(0, 3)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf3", args, sleep_time, datetime.datetime.now()))
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf3 done at %s\n" % datetime.datetime.now())
return sum(*args)
def smap(f, *arg):
if len(arg) == 2:
args, kwargs = arg
return f(list(args), **kwargs)
elif len(arg) == 1:
args = arg
return f(*args)
if __name__ == '__main__':
# Define the dataset
dataset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# Output the dataset
print ('Dataset: ' + str(dataset))
# Run this with a pool of 5 agents having a chunksize of 3 until finished
agents = 5
chunksize = 3
with Pool(processes=agents) as pool:
result = pool.map(square, dataset)
print("Result of Squares : %s\n\n" % result)
with Pool(processes=3) as pool:
result = pool.starmap(smap, [(pf1, [1,2,3], {'a':123, 'b':456}), (pf2, [11,22,33]), (pf3, [111,222,333])])

16
pool2.py

@ -0,0 +1,16 @@
# Quelle: https://stackoverflow.com/questions/35528093/multithreading-in-python-with-a-threadpool
from multiprocessing import Pool
import sys
if __name__ == '__main__':
job_list = [range(10000000)]*6
print(f'{job_list}')
if 'p' in sys.argv:
p = Pool(2)
print("Parallel map")
print(p.map(sum, job_list))
else:
print("Sequential map")
print(map(sum, job_list))

20
pool3.py

@ -0,0 +1,20 @@
# Quelle: https://www.codesdope.com/blog/article/multiprocessing-using-pool-in-python/
import time
from multiprocessing import Pool
def square(x):
print(f"start process:{x}")
square = x * x
print(f"square {x}:{square}")
time.sleep(1)
print(f"end process:{x}")
if __name__ == "__main__":
starttime = time.time()
pool = Pool()
pool.map(square, range(0, 5))
pool.close()
endtime = time.time()
print(f"Time taken {endtime-starttime} seconds")

45
race_condition_async.py

@ -0,0 +1,45 @@
import asyncio
import random
import time
gesamtwert = 0
message = ''
async def calculate_sum(lock, thread_nummer):
global gesamtwert
global message
for i in range(100 + thread_nummer):
async with lock:
zwischen = gesamtwert
#Zufällige Wartezeit zwischen 0,1s und 0,5s
wartezeit = random.randint(1,5+thread_nummer)
time.sleep(0.1 * wartezeit)
zwischen += 1
async with lock:
gesamtwert = zwischen
message = f'Thread {thread_nummer} fertig'
print(f'Thread + {thread_nummer} abgearbeitet.')
async def main():
#async Lock erzeugen
lock = asyncio.Lock()
# drei Threads starten, um die Summe der Zahlen zu berechnen
threads = [asyncio.create_task(calculate_sum(lock, _+1)) for _ in range(3)]
#Warten auf alle Threads
await asyncio.gather(*threads)
print(f'gesamtwert={gesamtwert}')
print(f'message={message}')
if __name__ == '__main__':
asyncio.run(main())
Loading…
Cancel
Save