Skip to content

Instantly share code, notes, and snippets.

@habbes
Created April 27, 2020 05:06
Show Gist options
  • Save habbes/f811ffca8548e8049b73ebebd812a682 to your computer and use it in GitHub Desktop.
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
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