Is it possible to "contain" a slime or ant pit without killing the queen?

For some reason a slime pits entrance is blocked off “hallway down” making it impossible to get to the brain without digging and get constantly attacked while mining.
However this got me thinking, is it possible to just build a wall around the stairs so I could just farm them at leisure? or do they just spawn all over the place instead of exiting the stairs?
Because it would be really nice to set up camp next to a mutagen generator without having to worry about npcs getting eaten or being constantly interrupted by footsteps

I’m not sure how slime pits work, but ant hills (fungal mounds and triffid groves too) have a radius or perimeter, within which their type of creature will spawn. I think the chance goes down as you get further away. I think ant hills have a modest range, something like 200 tiles, not sure exactly. This is why you can clear out the area around an ant hill, go to sleep, and wake up to 10X more ants than you started with.