Skip to content

Add Semaphore/Resource type #53

@maxfischer2781

Description

@maxfischer2781

Porting SimPy resources (#51 and #50) made it pretty obvious that uSim's Resources type is a bit awkward when there really is just one type of resources.

resources = Resources(capacity=8)
await resource.increase(capacity=8)
await resource.decrease(capacity=8)
async with resource.borrow(capacity=8):
    ...

Other ways to emulate a Semaphore are Tracked and Queue, which come with other unwanted baggage. IMO the Resources interface is good, but we may want a "just one type" version:

resources = Resource(8)
await resource.increase(8)
await resource.decrease(8)
async with resource.borrow(8):
    ...

The use-case of a raw Semaphore would be easily handled by having proper defaults:

resources = Resource()  # default level of 0
await resource.increase()  # default in/decrease of 1
await resource.decrease()
async with resource.borrow():  # default borrow/claim of 1
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions