sholloway / agents-playground

MIT License
4 stars 0 forks source link

Agent Simulation on Separate Thread from UI #2

Closed sholloway closed 1 year ago

sholloway commented 2 years ago

Use Case

Structure the code to farm off work to dedicated threads for specific types of work.

Simulation Requirements

Implementation Thoughts

Development Path

  1. Get the happy path of agents interacting using linear code.
  2. Profile
  3. Update to use threads/asyncio.
  4. Profile
  5. Potentially update to use subprocesses.

GUI Considerations

GUI Setup

Python Concurrency Options

Python Concurrency

Multi-Threading

Threads can be used to distribute work. The threading module is the high level module for working with threads.

The queue module provides thread safe queues for moving items between threads. There are FIFO, LIFO, and Priority queues.

Threads are not parallel processing. They leverage context switching. In CPython, due to the Global Interpreter Lock (GIL), only one thread can execute Python code at once. Threads are ideal for I/O bound use cases (e.g. writing to disk, network requests).

Async future callables can be spawned using concurrent.futures.

The asyncio module provides async/await functionality for working with Ccroutines. Asyncio programming consideration.

Multi-Processing

The multiprocessing module provides parallel processing for multi-core/processor machines. This modules uses sub-processes rather than threading. Threads remain within a single process. Each process has its own dedicated Python Interpreter.
You use the subprocess module for working with child processes.

Processes can be communicated with via Queues and Pipes. Examples

Multiprocessing Queue Parallel Processing programming guidelines

You can share a chunk of memory across processes using the multiprocessing.shared_memory module.