Python Petri Nets — Core Concepts

What a Petri Net Is

A Petri net is a mathematical model for describing concurrent systems. Invented by Carl Adam Petri in 1962, it consists of four components:

  1. Places — circles that hold tokens (represent conditions or resources)
  2. Transitions — rectangles/bars that fire when enabled (represent events or actions)
  3. Arcs — directed edges connecting places to transitions and transitions to places
  4. Tokens — dots inside places (represent the current state/marking)

The key rule: a transition is enabled (can fire) when every input place has at least as many tokens as the arc weight requires. When it fires, it removes tokens from input places and adds tokens to output places.

How Firing Works

Consider a simple net modeling a producer-consumer system:

  • Place P1: “items produced” (starts with 3 tokens)
  • Place P2: “buffer” (starts empty)
  • Transition T1: “move to buffer” (takes from P1, puts in P2)

When T1 fires: one token leaves P1, one token appears in P2. After three firings, P1 is empty and P2 has 3 tokens. T1 can no longer fire — it’s not enabled.

Why Not Just Use Flowcharts?

Flowcharts model sequential processes. Petri nets model concurrent ones. The critical difference:

FeatureFlowchartPetri Net
ParallelismOne active stepMultiple active tokens
SynchronizationNot expressibleFork and join naturally
Resource modelingAwkwardTokens represent resources
Deadlock detectionManual analysisMathematical properties
Formal analysisLimitedRich theory (reachability, liveness)

Key Properties

Reachability

Can the system reach a particular state? Given a starting configuration of tokens (the initial marking), which configurations are possible? This determines whether desired outcomes are achievable and undesired ones are avoidable.

Liveness

Will the system always be able to make progress? A transition is live if it can eventually fire again from any reachable state. A fully live Petri net guarantees no permanent deadlocks.

Boundedness

Do places accumulate unlimited tokens? A k-bounded net never has more than k tokens in any place. This ensures resources don’t grow without limit — critical for memory-constrained systems.

Deadlock Freedom

A deadlock occurs when no transition is enabled. Petri net analysis can prove whether a deadlock is possible without running every scenario.

Types of Petri Nets

Basic (Place/Transition) nets — tokens are indistinguishable black dots. Good for modeling control flow and synchronization.

Colored Petri Nets (CPNs) — tokens carry data (like colored marbles). Each token has a type and value. Transitions can inspect token values to decide whether to fire and how to transform data.

Timed Petri Nets — transitions or places have time delays. Useful for performance modeling — how long does the whole process take?

Stochastic Petri Nets — firing times are random variables. Used for reliability analysis and queueing models.

Python Libraries

LibraryTypeBest For
snakesGeneral Petri netsAcademic modeling, colored nets
pm4pyProcess mining with Petri netsDiscovering nets from event logs
PetriNet (custom)Simple implementationsLearning and small projects

Practical Applications

  • Workflow systems — model approval chains where multiple sign-offs must happen before proceeding
  • Manufacturing — model assembly lines with shared resources and parallel stages
  • Protocol verification — prove that a communication protocol can’t deadlock
  • Biological systems — model metabolic pathways and gene regulatory networks
  • Software architecture — verify that microservice choreographies don’t have race conditions

Common Misconception

“Petri nets are just state machines with extra steps.” State machines track one active state. Petri nets can have tokens in multiple places simultaneously, modeling true concurrency. A state machine can simulate a Petri net’s behavior, but the state space explodes — a net with 10 places, each holding 0-5 tokens, has 6¹⁰ ≈ 60 million possible states. Petri nets represent this compactly.

One thing to remember: Petri nets extend state machines by modeling concurrency — multiple tokens flowing through the network simultaneously — which makes them the right tool for reasoning about parallel processes, resource contention, and deadlocks.

pythonpetri-netsconcurrency

See Also

  • Python Finite Automata How finite automata work like vending machines — they read input step by step and decide whether to accept or reject based on simple rules.
  • Ci Cd Why big apps can ship updates every day without turning your phone into a glitchy mess — CI/CD is the behind-the-scenes quality gate and delivery truck.
  • Containerization Why does software that works on your computer break on everyone else's? Containers fix that — and they're why Netflix can deploy 100 updates a day without the site going down.
  • Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.
  • Python 311 New Features Python 3.11 made everything faster, error messages smarter, and let you catch several mistakes at once instead of stopping at the first one.