Mr.cysl
Mr.cysl

Reputation: 1624

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

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?

Upvotes: 8

Views: 4731

Answers (2)

oz1
oz1

Reputation: 998

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

Upvotes: 13

ev-br
ev-br

Reputation: 26110

You need to return a C++ structure: a struct, a vector, an std::pair, etc, not a python tuple object.

Upvotes: 0

Related Questions