Closed EJSohn closed 5 years ago
아래의 알고리즘 중 한가지를 원하는 프로그래밍 언어로 구현해보세요!
조건
import threading from queue import Queue import time import shutil print_lock = threading.Lock() def copy_op(file_data): with print_lock: print("Starting thread : {}".format(threading.current_thread().name)) mydata = threading.local() mydata.ip, mydata.op = next(iter(file_data.items())) shutil.copy(mydata.ip, mydata.op) with print_lock: print("Finished thread : {}".format(threading.current_thread().name)) def process_queue(): while True: file_data = compress_queue.get() copy_op(file_data) compress_queue.task_done() compress_queue = Queue() output_names = [{'v1.mp4' : 'v11.mp4'},{'v2.mp4' : 'v22.mp4'}] for i in range(2): t = threading.Thread(target=process_queue) t.daemon = True t.start() start = time.time() for file_data in output_names: compress_queue.put(file_data) compress_queue.join() print("Execution time = {0:.5f}".format(time.time() - start))
import threading from queue import Queue import requests import bs4 import time print_lock = threading.Lock() def get_url(current_url): with print_lock: print("\nStarting thread {}".format(threading.current_thread().name)) res = requests.get(current_url) res.raise_for_status() current_page = bs4.BeautifulSoup(res.text,"html.parser") current_title = current_page.select('title')[0].getText() with print_lock: print("{}\n".format(threading.current_thread().name)) print("{}\n".format(current_url)) print("{}\n".format(current_title)) print("\nFinished fetching : {}".format(current_url)) def process_queue(): while True: current_url = url_queue.get() get_url(current_url) url_queue.task_done() url_queue = Queue() url_list = ["https://www.google.com"]*5 for i in range(5): t = threading.Thread(target=process_queue) t.daemon = True t.start() start = time.time() for current_url in url_list: url_queue.put(current_url) url_queue.join() print(threading.enumerate()) print("Execution time = {0:.5f}".format(time.time() - start))
import threading from queue import Queue import time list_lock = threading.Lock() def find_rand(num): sum_of_primes = 0 ix = 2 while ix <= num: if is_prime(ix): sum_of_primes += ix ix += 1 sum_primes_list.append(sum_of_primes) def is_prime(num): if num <= 1: return False elif num <= 3: return True elif num%2 == 0 or num%3 == 0: return False i = 5 while i*i <= num: if num%i == 0 or num%(i+2) == 0: return False i += 6 return True def process_queue(): while True: rand_num = min_nums.get() find_rand(rand_num) min_nums.task_done() min_nums = Queue() rand_list = [1000000, 2000000, 3000000] sum_primes_list = list() for i in range(2): t = threading.Thread(target=process_queue) t.daemon = True t.start() start = time.time() for rand_num in rand_list: min_nums.put(rand_num) min_nums.join() end_time = time.time() sum_primes_list.sort() print(sum_primes_list) print("Execution time = {0:.5f}".format(end_time - start))
12/30(일) 영상 하나 보고오세요~ Concurrency is not parallelism
Content
아래의 알고리즘 중 한가지를 원하는 프로그래밍 언어로 구현해보세요!
조건
Filesystem I/O
Network I/O
CPU bound tasks