Multithreading and multiprocessing safety

Multithreading and multiprocessing safety#

General guidelines#

  • Every Wakepy Method is multiprocessing and multithreading safe. Wakepy by default does not alter any settings or have such side effects that would make it not possible have multiple wakepy Modes (inhibitors) active at the same time.

  • It is not safe to share Mode instances between threads (the ones returned by keep.running() and keep.presenting())

  • It is safe to share the functions decorated with wakepy factory functions with multiple threads, as each of them creates a new Mode internally.

Examples#

Examples of safe usage#

These are multithreading and multiprocessing safe ways using wakepy:

# Thread-safe because the decorated function long_running_function
# will create new Mode instance on every call.

@keep.running
def long_running_function(n):
    USER_CODE
# Also thread safe. Same reasons as above

@keep.running()
def long_running_function():
    USER_CODE
# Thread safe because each thread will get it's own Mode instance
# From keep.running()

def long_running_function(n):
    with keep.running():
        USER_CODE

Unsafe usage#

Any usage pattern sharing a Mode instance between threads is unsafe. The following is ok for single-threaded code, but not ok if long_running_function is running on a separate thread.

# Creating Mode instance on one thread
keepawake = keep.running()

@keepawake
def long_running_function():
    # UNSAFE! Using the Mode instance in separate thread
    print(f'Using: {keepawake.method}')

See also

current_mode() would be the correct way for getting the current Mode instance in a thread-safe way.