Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion can/interfaces/ics_neovi/neovi_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"""

import logging
import os
import tempfile
from collections import deque

from can import Message, CanError, BusABC
Expand All @@ -28,6 +30,35 @@
ics = None


try:
from filelock import FileLock
except ImportError as ie:

logger.warning(
"Using ICS NeoVi can backend without the "
"filelock module installed may cause some issues!: %s",
ie,
)

class FileLock:
"""Dummy file lock that does not actually do anything"""

def __init__(self, lock_file, timeout=-1):
self._lock_file = lock_file
self.timeout = timeout

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
return None


# Use inter-process mutex to prevent concurrent device open.
# When neoVI server is enabled, there is an issue with concurrent device open.
open_lock = FileLock(os.path.join(tempfile.gettempdir(), "neovi.lock"))


class ICSApiError(CanError):
"""
Indicates an error with the ICS API.
Expand Down Expand Up @@ -122,7 +153,9 @@ def __init__(self, channel, can_filters=None, **kwargs):
type_filter = kwargs.get("type_filter")
serial = kwargs.get("serial")
self.dev = self._find_device(type_filter, serial)
ics.open_device(self.dev)

with open_lock:
ics.open_device(self.dev)

if "bitrate" in kwargs:
for channel in self.channels:
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
long_description = f.read()

# Dependencies
extras_require = {"serial": ["pyserial~=3.0"], "neovi": ["python-ics>=2.12"]}
extras_require = {
"serial": ["pyserial~=3.0"],
"neovi": ["python-ics>=2.12", "filelock"],
}

tests_require = [
"pytest~=4.3",
Expand Down Expand Up @@ -90,6 +93,7 @@
"wrapt~=1.10",
"aenum",
'windows-curses;platform_system=="Windows"',
"filelock",
],
setup_requires=["pytest-runner"],
extras_require=extras_require,
Expand Down