Skip to content

@synchronized

Alex Sherman edited this page Dec 24, 2016 · 3 revisions

The second decorator found helps to simplify your concurrent programs. The @synchronzied decorator should be applied to functions that call @concurrent functions. Using @synchronized greatly simplifies concurrent programming, removing the need to manually call .get() on results.

Basic usage

Applying @synchronized to a function will alter all mentions of any @concurrent function. Instead of returning concurrent_results, @synchronized will attempt to alter calls in order to return the results directly and also apply any needed synchronization. This functionality does not work for every use case of @concurrent functions, but is supported for the following:

  • concurrent(...)
  • An expression that is a call of a concurrent function
  • other_func(concurrent(...), ...)
  • An expression where a function uses a concurrent function result as an argument
  • Any program state that other_func modifies will be modified at an arbitrary time but in the order that it was called
  • indexable[key] = concurrent(...)
  • An expression that is an assignment of a single indexable object at a specific key
@concurrent
def process(data):
    ...

@synchornized
def process_data_set(data_set):
  for data in data_set:
    process(data)

@synchronized
def process_data_set(data_set):
  results = []
  for data in data_set:
    results.append(data)
  return results

@synchornized
def process_data_set(data_set):
  results = {}
  for key, data in data_set:
    results[key] = process(data)
  return results

Unsupported Cases

When @synchronized detects that the function it is applied to contains an unsupported usage of @concurrent functions it will raise an exception. There are also some caveats and unsupported uses that the decorator cannot detect. Most of these corner cases are due to indirect references to data that is either modified or used by @concurrent functions. A list of some of these common pitfalls can be found in the side bar.

Clone this wiki locally