22
33import asyncio
44import concurrent .futures
5+ import threading
56import uuid
67from datetime import timedelta
78from typing import Any , Awaitable , Callable , Optional
@@ -351,6 +352,7 @@ def __init__(self, pnum: int):
351352 self .pnum = pnum
352353
353354 class MySlotSupplier (CustomSlotSupplier ):
355+ lock = threading .Lock ()
354356 reserves = 0
355357 releases = 0
356358 used = 0
@@ -364,7 +366,9 @@ async def reserve_slot(self, ctx: SlotReserveContext) -> SlotPermit:
364366 self .reserve_asserts (ctx )
365367 # Verify an async call doesn't bungle things
366368 await asyncio .sleep (0.01 )
367- self .reserves += 1
369+ with self .lock :
370+ self .reserves += 1
371+ print ("incremented reserve" )
368372 return MyPermit (self .reserves )
369373
370374 def try_reserve_slot (self , ctx : SlotReserveContext ) -> Optional [SlotPermit ]:
@@ -376,24 +380,26 @@ def mark_slot_used(self, ctx: SlotMarkUsedContext) -> None:
376380 assert isinstance (ctx .permit , MyPermit )
377381 assert ctx .permit .pnum is not None
378382 assert ctx .slot_info is not None
379- if isinstance (ctx .slot_info , WorkflowSlotInfo ):
380- self .seen_used_slot_kinds .add ("wf" )
381- elif isinstance (ctx .slot_info , ActivitySlotInfo ):
382- self .seen_used_slot_kinds .add ("a" )
383- elif isinstance (ctx .slot_info , LocalActivitySlotInfo ):
384- self .seen_used_slot_kinds .add ("la" )
385- self .used += 1
383+ with self .lock :
384+ if isinstance (ctx .slot_info , WorkflowSlotInfo ):
385+ self .seen_used_slot_kinds .add ("wf" )
386+ elif isinstance (ctx .slot_info , ActivitySlotInfo ):
387+ self .seen_used_slot_kinds .add ("a" )
388+ elif isinstance (ctx .slot_info , LocalActivitySlotInfo ):
389+ self .seen_used_slot_kinds .add ("la" )
390+ self .used += 1
386391
387392 def release_slot (self , ctx : SlotReleaseContext ) -> None :
388393 assert ctx .permit is not None
389394 assert isinstance (ctx .permit , MyPermit )
390395 assert ctx .permit .pnum is not None
391- # Info may be empty, and we should see both empty and not
392- if ctx .slot_info is None :
393- self .seen_release_info_empty = True
394- else :
395- self .seen_release_info_nonempty = True
396- self .releases += 1
396+ with self .lock :
397+ # Info may be empty, and we should see both empty and not
398+ if ctx .slot_info is None :
399+ self .seen_release_info_empty = True
400+ else :
401+ self .seen_release_info_nonempty = True
402+ self .releases += 1
397403
398404 def reserve_asserts (self , ctx ):
399405 assert ctx .task_queue is not None
@@ -422,9 +428,6 @@ def reserve_asserts(self, ctx):
422428 await wf1 .signal (WaitOnSignalWorkflow .my_signal , "finish" )
423429 await wf1 .result ()
424430
425- async def releases () -> int :
426- return ss .releases
427-
428431 assert ss .reserves == ss .releases
429432 # Two workflow tasks, one activity
430433 assert ss .used == 3
0 commit comments