Created
April 27, 2020 05:06
-
-
Save habbes/f811ffca8548e8049b73ebebd812a682 to your computer and use it in GitHub Desktop.
Checking whether an I/O bound thread gets a chance to run if there's a CPU bound thread using the CPU
This file contains 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
import multiprocessing | |
import threading | |
import time | |
import random | |
import math | |
def io_thread_run(val): | |
time.sleep(1) | |
while True: | |
print("thread ", val) | |
time.sleep(1) | |
def cpu_thread_run(val): | |
i = 0 | |
while True: | |
i += math.sin(i) * math.cos(i) | |
num_cpus = multiprocessing.cpu_count() | |
num_cpu_threads = 1 #num_cpus * 2 | |
num_io_threads = 1 #num_cpus | |
threads = [] | |
for i in range(num_cpu_threads): | |
threads.append(threading.Thread(target=cpu_thread_run, args=(i, ))) | |
for i in range(num_io_threads): | |
threads.append(threading.Thread(target=io_thread_run, args=(i,))) | |
print("Num CPUs", num_cpus) | |
for thread in threads: | |
thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment