Last active
August 7, 2023 09:50
-
-
Save ehzawad/f2722679e45b2694f96025fab7828f87 to your computer and use it in GitHub Desktop.
Python GIL stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```python | |
import asyncio | |
import os | |
import threading | |
async def print_odd(max_value): | |
for i in range(max_value): | |
if i % 2 != 0: | |
asyncio.sleep(1) | |
print(i) | |
async def print_even(max_value): | |
for i in range(max_value): | |
if i % 2 == 0: | |
asyncio.sleep(3) | |
print(i) | |
async def main(): | |
await asyncio.gather(print_odd(5), print_even(5)) | |
await asyncio.gather(print_even(5), print_odd(5)) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
loop.close() | |
from multiprocessing import Pool | |
import time | |
def fib(n): | |
if n <= 2: | |
return 1 | |
else: | |
a, b = 1, 1 | |
for i in range(2, n): | |
a, b = b, a + b | |
return b | |
if __name__ == "__main__": | |
# create a multiprocessing Pool | |
with Pool(5) as p: | |
numbers = [30, 135, 240, 10545] | |
start_time = time.time() | |
result = p.map(fib, numbers) | |
for num, fib_val in zip(numbers, result): | |
print(f"Fibonacci({num}) = {fib_val}") | |
end_time = time.time() | |
print(f"Total execution time: {end_time - start_time} seconds") | |
from multiprocessing import Pool, current_process, cpu_count | |
import os, time | |
def io_bound_task(num): | |
start_time = time.time() | |
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Starting Task {num}") | |
time.sleep(2) # simulate I/O-bound task with delay | |
end_time = time.time() | |
print(f"Process ID: {os.getpid()}, Process Name: {current_process().name}, Finished Task {num}, Execution time: {end_time - start_time} seconds") | |
if __name__ == "__main__": | |
st = time.time() | |
num_processes = cpu_count() # get number of available processors | |
with Pool(num_processes) as p: | |
tasks = range(num_processes) # 10 tasks to be done | |
p.map(io_bound_task, tasks) | |
et = time.time() | |
print(f"Entire execution time: {et - st} seconds") | |
import os, time | |
from multiprocessing import cpu_count | |
def io_bound_task(num): | |
start_time = time.time() | |
print(f"Task ID: {num}, Starting Task {num}") | |
time.sleep(2) # simulate I/O-bound task with delay | |
end_time = time.time() | |
print(f"Task ID: {num}, Finished Task {num}, Execution time: {end_time - start_time} seconds") | |
if __name__ == "__main__": | |
st = time.time() | |
num_processes = cpu_count() # get number of available processors | |
tasks = range(num_processes) # 10 tasks to be done | |
for task in tasks: | |
io_bound_task(task) | |
et = time.time() | |
print(f"Entire execution time: {et - st} seconds") | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment