hardbyte / hardbyte/python-can

thread_safe_bus.state (getter) should not use locks

Đang mở
#1,891 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
1.6k
Fork
697
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

### Problem description
I have a small application that listens on one thread, and may send on another (using asyncio). Before sending, I used to check the hardware state by evaluating the `.state` property. I use the thread safe bus.
However, this leads to long wait phases, depending on _incoming_ messages.

It turns out that getting the `.state` property locks both send and receive locks, whereas `lock_recv` is probably occupied by the listener most of the time, which causes the delays.
in `thread_safe_bus.py`:
```
@property
def state(self):
with self._lock_send, self._lock_recv:
return self.__wrapped__.state
```

### Proposed change

I am not very familiar with thread-safe communication in Python, but derived from my C++ knowledge, the value of the `.state` property is only an _enum value_ and should be _atomic_ anyway, especially when reading.
So from my point of view, I would either just return the value without locks. Or - if any locking is needed for some reason - use a separate state-access-lock that is independent from `lock_send` and `lock_recv`.

### Workaround

I guess checking the bus state before each `send()` call is not the correct way to do. I switched to a mere `send()` and catch a `CanError` exception afterwards, which works fine without delay.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.