Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As soon as you leave python, that C extension can give up the GIL and your other python threads can start running.

The GIL makes python code have cooperative threading. It does not protect from e.g. your thread's view of state mutating when you make a database call.

I also believe it is best practice not to mutate data without holding the GIL in extension code, not a requirement - but I have mucked with a lot of different extension API so I might be confused.



No, I wouldn’t call it cooperative threading, it’s still preemptive in that any Python thread can be switched with another at any instruction. That’s the same behavior as the operating system with the same potential for race conditions (except python instructions are higher level than machine code instructions.)

While C extensions can release the GIL, that only makes sense if they do enough work that a Python thread could get some things done in the meanwhile, and it wouldn’t be surprising to the caller. Obviously the C thread can’t interact with the Python world after the GIL has been released.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: