0

This question is related to my old one (Safe writing to variable in cython c wrapper within two python processes or distinct memory for python processes)

I have custom class inherited from collections.deque with some additional logic:

from collections import deque
from threading import Lock

class DequeWithLock(deque):
    def __init__(self, iterable=(), maxlen=None):
        super().__init__(iterable, maxlen)
        self.lock = Lock()

    def safe_append(self, data):
        try:
            self.lock.acquire()
            self.append(data)
        finally:
            self.lock.release()

I also have in some sort "main" cdef class named SubscriptionClass. The strange thing is that everything works fine if I write this class as following:

from dxpyfeed.wrapper.utils.data_class import DequeWithLock as deque

cdef class SubscriptionClass:
    cdef dict data

    def __init__(self):
        self.data = dict()
        self.data['data'] = deque()

This code works as expected and even Lock() works as supposed to. However I'd like to get rid of unnecessary dict definition, so I rewrite to code in the following way:

cdef class SubscriptionClass:
    cdef object data

    def __init__(self):
        self.data = deque()

But this throws me a message every time a Cython-function started in another thread tries to use SubscriptionClass.data and no elements are appended to deque. The message: Exception ignored in: 'path.to.cython.function'.

I also tried to add cdef dict __dict__ from (Mixing cdef and regular python attributes in cdef class) but this gives me dead kernel.

Ivan Mishalkin
  • 1,049
  • 9
  • 25
  • Where you get `Exception ignored` can you add `try: whatever_causes_the_exception(); except: traceback.print_exc(); raise`. That should at least tell you what the exception was that's been ignored – DavidW Dec 06 '19 at 17:16
  • Thinking about it though - it might just be that `data` isn't public (Cython attributes aren't, by default) so your other thread can't access it. `cdef public object data` – DavidW Dec 06 '19 at 17:18
  • Sorry, there was an error in `path.to.cython.function` so I got Exception ignored because of it – Ivan Mishalkin Dec 10 '19 at 10:20
  • Has it been solved? I think @DavidW answer is accurate. – Jim Jun 27 '23 at 07:03

0 Answers0