Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return two values in cython cdef without gil (nogil)

Tags:

python

cython

I have a function and I am trying to return a number and a vector of ints. What I have is

cdef func() nogil:
    cdef vector[int] vect
    cdef int a_number
    ...
    return a_number, vect

but this will give errors like Assignment of Python object not allowed without gil. Is there a workaround?

like image 516
Mr.cysl Avatar asked Feb 19 '19 05:02

Mr.cysl


1 Answers

Cython has a ctuple type http://docs.cython.org/en/latest/src/userguide/language_basics.html#types

%%cython -a -f -+
from libcpp.vector cimport vector

cdef (vector[int], double) func() nogil:
    cdef vector[int] vec
    cdef double d = 3.14
    cdef int i
    for i in range(10):
        vec.push_back(i)
    return vec, d
like image 165
oz1 Avatar answered Sep 24 '22 16:09

oz1