<jupyter_start><jupyter_text>High Performance Data Science (HPDS)https://taudata.blogspot.comHPDS-02: Pendahuluan Thread Programming di Pythonhttps://taudata.blogspot.com/2022/04/hpds-02.html(C) <NAME><jupyter_code># -*- coding: utf-8 -*-
"""
Created on Mon Dec  7 08:59:27 2020
Contoh Threading sederhana di Python 3
@author: <NAME>
"""
import time
from threading import Thread

def sleeper(i):
    nSleep = 3
    print("thread {} sleeps for {} seconds".format(i, nSleep))
    time.sleep(nSleep)
    print("thread %d woke up" % i)

if __name__ == "__main__":
    for i in range(10):
        t = Thread(target=sleeper, args=(i,))
        t.start()
import threading

def print_cube(num):
    print("Cube: {}".format(num * num * num))

def print_square(num):
    print("Square: {}".format(num * num))

if __name__ == "__main__":
    # creating thread
    t1 = threading.Thread(target=print_square, args=(10,))
    t2 = threading.Thread(target=print_cube, args=(10,))
    t1.start()  # starting thread 1
    t2.start()  # starting thread 2

    t1.join()  # wait until thread 1 is completely executed
    t2.join()  # wait until thread 2 is completely executed
    # both threads completely executed
    print("Done!")
import requests
import time

def download_site(url, session):
    with session.get(url) as response:
        print(f"Read {len(response.content)} from {url}")

def download_all_sites(sites):
    with requests.Session() as session:
        for url in sites:
            download_site(url, session)

if __name__ == "__main__":
    sites = [
        "https://www.detik.com",
        "https://kompas.com",
    ] * 80
    start_time = time.time()
    download_all_sites(sites)
    duration = time.time() - start_time
    print(f"Downloaded {len(sites)} in {duration} seconds")
import concurrent.futures
import requests
import threading
import time

thread_local = threading.local()

def get_session():
    if not hasattr(thread_local, "session"):
        thread_local.session = requests.Session()
    return thread_local.session

def download_site(url):
    session = get_session()
    with session.get(url) as response:
        print(f"Read {len(response.content)} from {url}")

def download_all_sites(sites):
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        executor.map(download_site, sites)

if __name__ == "__main__":
    sites = [
        "https://detik.com",
        "https://kompas.com",
    ] * 80
    start_time = time.time()
    download_all_sites(sites)
    duration = time.time() - start_time
    print(f"Downloaded {len(sites)} in {duration} seconds")
# -*- coding: utf-8 -*-
"""
Created on Mon Dec  7 12:59:23 2020
Ilustrasi Pengaruh GIL (single thread)
@author: <NAME>
"""
# single_threaded.py
import time

COUNT = 50000000

def countdown(n):
    while n>0:
        n -= 1

start = time.time()
countdown(COUNT)
end = time.time()

print('Time taken in seconds -', end - start)
# -*- coding: utf-8 -*-
"""
Created on Mon Dec  7 12:59:23 2020
Ilustrasi Pengaruh GIL (Multi thread)
Akan menghasilkan waktu yang kurang lebih sama
@author: <NAME>
"""
import time
from threading import Thread

COUNT = 50000000

def countdown(n):
    while n>0:
        n -= 1

t1 = Thread(target=countdown, args=(COUNT//2,))
t2 = Thread(target=countdown, args=(COUNT//2,))

start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()

print('Time taken in seconds -', end - start)
# -*- coding: utf-8 -*-
"""
Created on Mon Dec  7 13:03:30 2020
Contoh sederhana multi-processing
@author: <NAME>
"""
from multiprocessing import Pool
import time

COUNT = 50000000
def countdown(n):
    while n>0:
        n -= 1

if __name__ == '__main__':
    pool = Pool(processes=2)
    start = time.time()
    r1 = pool.apply_async(countdown, [COUNT//2])
    r2 = pool.apply_async(countdown, [COUNT//2])
    pool.close()
    pool.join()
    end = time.time()
    print('Time taken in seconds -', end - start)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1399145943230344, 'fraction_numerical': 0.031147952775684502, 'mean_word_length': 3.4541387024608503, 'pattern_counts': {'":': 4, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '419475', 'license': 'Apache-2.0', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1359, 'n_words': 362, 'path': 'hpds-02-Code.ipynb', 'repo_name': 'taudata-indonesia/elearning', 'size': '4021'}