Igor Korotach
Head of FinTech at Quantum
Written by: Igor Korotach
def main():
num = 10
num_reference = None
# num_pointer = num
print(num_reference)
if __name__ == '__main__':
main()
#include <stdio.h>
int main() {
int num = 10;
int *num_pointer = NULL;
// num_pointer = #
printf("%d",*num_pointer);
return 0;
}
None
Process finished with exit code 0
Process finished with exitcode 139
(interrupted by signal 11: SIGSEGV)
// SIGSEGV - segmentation fault
def main():
array = [1, 2, 3]
print(array[3])
if __name__ == '__main__':
main()
#include <stdio.h>
int main(void) {
int arr[2];
arr[3] = 10;
printf("%d", arr[3]);
return 0;
}
Traceback (most recent call last):
File ".../main.py", line 6, in <module>
main()
File ".../main.py", line 3, in main
print(array[3])
~~~~~^^^
IndexError: list index out of range
70303616
Process finished with exit code 0
# I protect all the GC, Reference Counting, Memory Allocation, etc.
mutex = GlobalPythonMutex()
Thread 1:
mutex.lock()
# I do a lot of Python work here!
mutex.unlock()
Thread 2:
mutex.lock()
# Now I do a lot of Python work!
mutex.unlock()
/* Implement pysleep() for various platforms.
When interrupted
(or when another error occurs),
return -1 and
set an exception; else return 0. */
static int
pysleep(_PyTime_t secs)
{
// ...
/* Allow sleep(0) to maintain win32
* semantics, and as decreed
* by Guido, only the main thread can
* be interrupted.
*/
ul_millis = (unsigned long)millisecs;
if (ul_millis == 0 || !_PyOS_IsMainThread()) {
Py_BEGIN_ALLOW_THREADS
Sleep(ul_millis);
Py_END_ALLOW_THREADS
break;
}
// ...
return 0;
}
The GIL provides an important simplifying model of object access (including refcount manipulation) because it ensures that only one thread of execution can mutate Python objects at a time.
Moreover, synchronising objects to safely use them across parallel threads adds performance problems to single-threaded applications.
By Igor Korotach