Make proper use of threading.local()

The client and pipeline weren't guaranteed to be available on other threads than the main. Now the client and pipeline should be properly set up on each thread.