def get_stack_sampler() -> StackSampler:
    """
    Gets the stack sampler for the current thread, creating it if necessary
    """
    try:
        return thread_locals.stack_sampler
    except AttributeError:
        # Attribute 'stack_sampler' doesn't exist in thread_locals, create it
        stack_sampler = StackSampler()
        thread_locals.stack_sampler = stack_sampler
        return stack_sampler

    Gets the stack sampler for the current thread, creating it if necessary
    
pyinstrument/stack_sampler.py