Skip to main content

Create atomic loop iterations (let the iteration finish even when interrupted)

Project description

show example | jump to docs

Create atomic loop iterations (let the current iteration finish even when interrupted).

A simple context manager that asynchronously listens to potential SIGINT and SIGTERM and provides you with plain boolean flag that can be used to control the iteration flow of a while loop based on whether or not an interrupt/terminate signal was received.

This was formerly built for reinforcement learning library that trains its agents in iterations and has to gracefully finish the current running episode every time the training is interrupted.

It will be most useful in similar use cases, but can be actually used for anything else. Technically, any block of code that needs to run without an interruption.

Note that you will still be able to kill the running process using SIGKILL.

Getting Started

pip3 install atomicloop

The following example loop runs 3 iterations, each lasting three seconds. You can try interrupting one of the steps by pressing Ctrl+C (which sends SIGINT to the process). You will see the signal feedback but the program will wait for the current iteration to finish and exits after the loop iteration is done.

Moreover, you can see the context manager is aware whether or not it finished due to an interruption.

import time
from atomicloop import AtomicLoop


# Optional callback handlers ----------------------------------------
def on_signal(signal, frame):
print(f'  Received signal: {signal}')
print('  Finishing current step before terminating')


def on_exit(was_interrupted):
print('  Ended training')
print(f'  Interrupted? {was_interrupted}')


# Running the loop --------------------------------------------------
goal_steps = 3

with AtomicLoop(on_signal, on_exit) as loop:
        step_no = 0
        while loop.run and step_no < goal_steps:
                print(f'Starting training step #{step_no}')
                time.sleep(3) # simulating some code execution
                print(f'Finished training step #{step_no}')
                print('----------------------------------\n')
                step_no += 1

Documentation

atomicloop.AtomicLoop(on_signal=None, on_end=None)

The main and only class that creates the desired context manager.

on_signal is a callable that gets invoked on incoming SIGINT or SIGTERM. It is called with 2 arguments: the signal number and the current stack frame. For details on the arguments, please consult handler part in official signal.signal documentation.

on_end is a callable that gets invoked after exiting the context (after with block). It is called with a single boolean argument stating whether or not the execution was interrupted.

AtomicLoop() returns a new instance. To access the boolean flag for flow control, you have to read the flag as the instance’s attribute.

The .run boolean flag states whether or not the program should continue its execution. It is True by default and switches to False when an interruption signal was received.

You can also use one of many synonymous attributes:

  • loop.run

  • loop.loop

  • loop.move

  • loop.cont

  • loop.keep

  • loop.keep_going

  • loop.uninterrupted

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

atomicloop-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

atomicloop-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file atomicloop-1.0.0.tar.gz.

File metadata

  • Download URL: atomicloop-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for atomicloop-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5bc0934ac2b36f25b53f8fab692d94ce387c4fc548cd08de9e3cef5f64acb2c7
MD5 954c6ec0c627a922209d9b4ebf7b6ed7
BLAKE2b-256 0def3877b173ab2b5349bd95d05d5451a7aeefd6de3b64b7cc9c2bc08b88f4e6

See more details on using hashes here.

File details

Details for the file atomicloop-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for atomicloop-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 370bd80775bc072d06f4505b80c0096417654136fd46c3722b263bcca9777ac5
MD5 66abb895b395b850f36a132cc98f570d
BLAKE2b-256 24cad9fc4bd21de35633963dcdeb6efbb30ab5de740a4fa0c4157f2a9f3350b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page