Skip to content

Commit cb4a197

Browse files
authored
Add files via upload
1 parent a67334d commit cb4a197

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Multiprocessing with ProcessPoolExecutor
2+
3+
from concurrent.futures import ProcessPoolExecutor
4+
import time
5+
6+
def square_number(number):
7+
time.sleep(2)
8+
return f"Square: {number * number}"
9+
10+
numbers=[1,2,3,4,5,6,7,8,9,11,2,3,12,14]
11+
if __name__=="__main__":
12+
13+
with ProcessPoolExecutor(max_workers=3) as executor:
14+
results=executor.map(square_number,numbers)
15+
16+
for result in results:
17+
print(result)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Multithreading With Thread Pool Executor
2+
3+
from concurrent.futures import ThreadPoolExecutor
4+
import time
5+
6+
def print_number(number):
7+
time.sleep(1)
8+
return f"Number :{number}"
9+
10+
numbers=[1,2,3,4,5,6,7,8,9,0,1,2,3]
11+
12+
with ThreadPoolExecutor(max_workers=3) as executor:
13+
results=executor.map(print_number,numbers)
14+
15+
for result in results:
16+
print(result)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Real-World Example: Multiprocessing for CPU-bound Tasks
3+
Scenario: Factorial Calculation
4+
Factorial calculations, especially for large numbers,
5+
involve significant computational work. Multiprocessing
6+
can be used to distribute the workload across multiple
7+
CPU cores, improving performance.
8+
9+
'''
10+
11+
import multiprocessing
12+
import math
13+
import sys
14+
import time
15+
16+
# Increase the maximum number of digits for integer conversion
17+
sys.set_int_max_str_digits(100000)
18+
19+
## function to compute factorials of a given number
20+
21+
def computer_factorial(number):
22+
print(f"Computing factorial of {number}")
23+
result=math.factorial(number)
24+
print(f"Factorial of {number} is {result}")
25+
return result
26+
27+
if __name__=="__main__":
28+
numbers=[5000,6000,700,8000]
29+
30+
start_time=time.time()
31+
32+
##create a pool of worker processes
33+
with multiprocessing.Pool() as pool:
34+
results=pool.map(computer_factorial,numbers)
35+
36+
end_time=time.time()
37+
38+
print(f"Results: {results}")
39+
print(f"Time taken: {end_time - start_time} seconds")
40+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## PRocesses that run in parallel
2+
### CPU-Bound Tasks-Tasks that are heavy on CPU usage (e.g., mathematical computations, data processing).
3+
## PArallel execution- Multiple cores of the CPU
4+
5+
import multiprocessing
6+
7+
import time
8+
9+
def square_numbers():
10+
for i in range(5):
11+
time.sleep(1)
12+
print(f"Square: {i*i}")
13+
14+
def cube_numbers():
15+
for i in range(5):
16+
time.sleep(1.5)
17+
print(f"Cube: {i * i * i}")
18+
19+
if __name__=="__main__":
20+
21+
## create 2 processes
22+
p1=multiprocessing.Process(target=square_numbers)
23+
p2=multiprocessing.Process(target=cube_numbers)
24+
t=time.time()
25+
26+
## start the process
27+
p1.start()
28+
p2.start()
29+
30+
## Wait for the process to complete
31+
p1.join()
32+
p2.join()
33+
34+
finished_time=time.time()-t
35+
print(finished_time)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
### Multithreading
2+
## When to use Multi Threading
3+
###I/O-bound tasks: Tasks that spend more time waiting for I/O operations (e.g., file operations, network requests).
4+
### Concurrent execution: When you want to improve the throughput of your application by performing multiple operations concurrently.
5+
6+
import threading
7+
import time
8+
9+
def print_numbers():
10+
for i in range(5):
11+
time.sleep(2)
12+
print(f"Number:{i}")
13+
14+
def print_letter():
15+
for letter in "abcde":
16+
time.sleep(2)
17+
print(f"Letter: {letter}")
18+
19+
##create 2 threads
20+
t1=threading.Thread(target=print_numbers)
21+
t2=threading.Thread(target=print_letter)
22+
23+
t=time.time()
24+
## start the thread
25+
t1.start()
26+
t2.start()
27+
28+
### Wait for the threads to complete
29+
t1.join()
30+
t2.join()
31+
32+
finished_time=time.time()-t
33+
print(finished_time)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Real-World Example: Multithreading for I/O-bound Tasks
3+
Scenario: Web Scraping
4+
Web scraping often involves making numerous network requests to
5+
fetch web pages. These tasks are I/O-bound because they spend a lot of
6+
time waiting for responses from servers. Multithreading can significantly
7+
improve the performance by allowing multiple web pages to be fetched concurrently.
8+
9+
'''
10+
11+
'''
12+
13+
https://python.langchain.com/v0.2/docs/introduction/
14+
15+
https://python.langchain.com/v0.2/docs/concepts/
16+
17+
https://python.langchain.com/v0.2/docs/tutorials/
18+
'''
19+
20+
import threading
21+
import requests
22+
from bs4 import BeautifulSoup
23+
24+
urls=[
25+
'https://python.langchain.com/v0.2/docs/introduction/',
26+
27+
'https://python.langchain.com/v0.2/docs/concepts/',
28+
29+
'https://python.langchain.com/v0.2/docs/tutorials/'
30+
31+
]
32+
33+
def fetch_content(url):
34+
response=requests.get(url)
35+
soup=BeautifulSoup(response.content,'html.parser')
36+
print(f'Fetched {len(soup.text)} characters from {url}')
37+
38+
threads=[]
39+
40+
for url in urls:
41+
thread=threading.Thread(target=fetch_content,args=(url,))
42+
threads.append(thread)
43+
thread.start()
44+
45+
for thread in threads:
46+
thread.join()
47+
48+
print("All web pages fetched")

0 commit comments

Comments
 (0)