Changeset 1197
- Timestamp:
- 02/05/23 22:48:52 (21 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/frams.py
r1189 r1197 17 17 If you want to run many independent instances of this class in parallel, use the "multiprocessing" module and then each process 18 18 that uses this module will initialize it and get access to a separate instance of the Framsticks library. 19 20 If you want to use this module from multiple threads concurrently, use the "-t" option for init(). 21 This will make concurrent calls from different threads sequential, thus making them safe. 22 However, this will likely degrade the performance (due to required locking) compared to the single-threaded use. 19 23 20 24 For interfaces in other languages (e.g. using the Framsticks library in your C++ code), see ../cpp/frams/frams-objects.h … … 308 312 '-LNAME' is the optional library name (full name including the file name extension), default is 'frams-objects.dll/.so' depending on the platform. 309 313 All other arguments are passed to Framsticks and not interpreted by this function. 310 311 314 """ 312 # goals: 315 313 316 frams_d = None 314 317 frams_D = None … … 323 326 elif a[:2] == '-L': 324 327 lib_name = a[2:] 328 elif a[:2] == '-t': 329 print("frams.py: thread synchronization enabled.") # Due to performance penalty, only use if you are really calling methods from different threads. 330 from functools import wraps 331 from threading import RLock 332 333 def threads_synchronized(lock): 334 def wrapper(f): 335 @wraps(f) 336 def inner_wrapper(*args, **kwargs): 337 with lock: 338 return f(*args, **kwargs) 339 return inner_wrapper 340 return wrapper 341 342 thread_synchronizer = threads_synchronized(RLock()) 343 for name in ExtValue.__dict__: 344 attr = getattr(ExtValue, name) 345 if callable(attr) and attr: # decorate all methods of ExtValue with a reentrant lock so that different threads do not use them concurrently 346 setattr(ExtValue, name, thread_synchronizer(attr)) 325 347 elif lib_path is None: 326 348 lib_path = a … … 340 362 abs_data = os.path.join(os.path.abspath(lib_path), "data") # use absolute path for -d and -D so python is free to cd anywhere without confusing Framsticks 341 363 342 # for the hypothetical case without lib_path the abs_data must be obtained from somewhere else364 # for the hypothetical case without lib_path, the abs_data must be obtained from somewhere else 343 365 if frams_d is None: 344 366 frams_d = '-d' + abs_data
Note: See TracChangeset
for help on using the changeset viewer.