Problems with Forking while using VSTs in a DAW

I’m creating a drum beat using a drumset VST in Reaper
I created a function to play the hi hat part and a backbeat function
But when I try to fork them both, I get this horrible screeching sound and the error:

WARNING:root:Ran out of threads in the master clock’s ThreadPool; small thread creation delays may result. You can increase the number of threads in the pool to avoid this.

My code:

from scamp import *
import random

s = Session(tempo=100)

drums = s.new_midi_part("drums", 2)

def play_hi_hat():
    i = random.uniform(0,1)
    if i > 0.2:
        drums.play_note(42, 1, 0.5)
    else:
        drums.play_note(42, 1, 0.25)
        drums.play_note(42, 1, 0.25)
        
def play_backbeat():
    drums.play_note(36,1,1)
    drums.play_note(38,1,1)

while True:
    s.fork(play_hi_hat)
    s.fork(play_backbeat)

Even if I run the s.fork(play_hi_hat) without the other function, the screeching occurs and the error appears.

Calls to fork are instantaneous (non-blocking), so this code is going to start forking play_hi_hat and play_backbeat over and over as fast as possible! (Hence the screeching.) You need to add a wait call, something like:

while True:
    s.fork(play_hi_hat)
    s.fork(play_backbeat)
    wait(2)

Or perhaps:

while True:
    s.fork(play_hi_hat)
    s.fork(play_backbeat)
    s.wait_for_children_to_finish()

Which will wait for both forked parts to finish before forking them again.

Thank you Marc, it works perfectly now!