Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython: How to print without GIL

How should I use print in a Cython function with no gil? For example:

from libc.math cimport log, fabs
cpdef double f(double a, double b) nogil:
    cdef double c = log( fabs(a - b) )
    print c
    return c

gives this error when compiling:

Error compiling Cython file:
...
    print c
    ^
------------------------------------------------------------

Python print statement not allowed without gil
...

I know how to use C libraries instead of their python equivalent (math library for example here) but I couldn't find a similar way for print.

like image 349
Behzad Jamali Avatar asked Jan 21 '18 03:01

Behzad Jamali


1 Answers

Use printf from stdio:

from libc.stdio cimport printf
...
printf("%f\n", c)
like image 73
a spaghetto Avatar answered Sep 20 '22 04:09

a spaghetto