diff --git a/Practice/Karpov_A/Lec9/t9_1.py b/Practice/Karpov_A/Lec9/t9_1.py new file mode 100644 index 00000000..6b4f3f3d --- /dev/null +++ b/Practice/Karpov_A/Lec9/t9_1.py @@ -0,0 +1,34 @@ +import time +import threading + + +def find_primes(start, end): + primes = [] + start_time = time.time() + for i in range(start, end + 1): + if i > 1: + for n in range(2, i): + if (i % n) == 0: + break + else: + primes.append(i) + print(f"Диапазон от {start} до {end}, время обработки {time.time() - start_time} сек.") + return primes + +begin = [3, 10001, 20001] +finish = [10000, 20000, 30000] + +start_seq = time.time() +for i in range(3): + find_primes(begin[i], finish[i]) +print(f"Общее время: {time.time() - start_seq} сек.") + +start_thr = time.time() +threads = [] +for i in range(3): + thread = threading.Thread(target=find_primes, args=(begin[i], finish[i])) + threads.append(thread) + thread.start() +for thr in threads: + thr.join() +print(f"Многопоцессорность: {time.time() - start_thr}") \ No newline at end of file diff --git a/Practice/Karpov_A/Lec9/t9_2.py b/Practice/Karpov_A/Lec9/t9_2.py new file mode 100644 index 00000000..e41067ab --- /dev/null +++ b/Practice/Karpov_A/Lec9/t9_2.py @@ -0,0 +1,36 @@ +from multiprocessing import Process +import time + +def add(*args): + start_time = time.time() + if isinstance(args[0], int) or isinstance(args[0], float): + back = 0 + elif isinstance(args[0], list): + back = list() + else: + back = '' + + for elem in args: + back += elem + + print('Сложение для {}, затрачено {} sec.'.format(type(args[0]), time.time() - start_time)) + print(back) + +if __name__ == '__main__': + args = [('aaa', 'bbbb', 'cccc'), + (1, 2, 3, 4,), + (1.1, 2.2, 3.3, 4.4,), + ([1, 2, 3], ['asd', 'rrr'], [10.3, 2.2],)] + + def my_proc(func, args): + multiprocess = [] + for arg in args: + multiprocess.append(Process(target=func, args=arg)) + for p in multiprocess: + p.start() + yield p + + new_list = list(my_proc(add, args)) + + for p in new_list: + p.join() \ No newline at end of file diff --git a/Practice/Karpov_A/t7_3.py b/Practice/Karpov_A/t7_3.py new file mode 100644 index 00000000..d3dec501 --- /dev/null +++ b/Practice/Karpov_A/t7_3.py @@ -0,0 +1,37 @@ +import random +import pickle + +class Human(): + def __init__(self, first_name, last_name, age, hair_color, p_of_residence): + self.first_name = first_name + self.last_name = last_name + self.age = age + self.hair_color = hair_color + self.p_of_residence = p_of_residence + def __str__(self): + return f"Human: first_name={self.first_name}, last_name={self.last_name}, age={self.age}, hair_color={self.hair_color}," \ + f" p_of_residence={self.p_of_residence}" + +def fun(num): + f_name = ['Anton', 'Ksenia', 'Serg', 'Pavel', 'Cemen'] + l_name = ['Abramov', 'Kitov', 'Block', 'Kazakov', 'Ivanov'] + p_of_r = ['Moscow', 'Nizhny Novgorod', 'Vladimir', 'St. Petersburg', 'Kazan'] + hair_color = ['Green', 'Red', 'Blue', 'Gray', 'Black'] + people = [] + for i in range(num): + people.append(Human(random.choice(f_name), random.choice(l_name), random.randint(1, 115), random.choice(hair_color), random.choice(p_of_r))) + for i in people: + print(i) + with open('data.pickle', 'wb') as f: + pickle.dump(people, f) + return people + +def fun1(file): + with open(file, 'rb') as f: + data_new = pickle.load(f) + for line in data_new: + print(line) + return data_new + +fun(5) +fun1('data.pickle') \ No newline at end of file