py: fix a bug leads to deadlock

release GIL before do some operation that
acquiring lock