Add mutex locking to prevent issues with embedded python