File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ Create your routines:
2121# basic.py
2222from time import sleep as time_sleep
2323
24- from queueio import QueueIO
24+ from queueio import activate
2525from queueio import routine
2626from queueio.gather import gather
2727from queueio.sleep import sleep
@@ -43,11 +43,8 @@ async def yielding(iterations: int):
4343
4444
4545if __name__ == " __main__" :
46- q = QueueIO()
47- try :
48- q.submit(yielding(7 ))
49- finally :
50- q.shutdown()
46+ with activate():
47+ yielding(7 ).start()
5148
5249```
5350
Original file line number Diff line number Diff line change 1- from .queueio import QueueIO as QueueIO
1+ from contextlib import contextmanager
2+
3+ from .queueio import QueueIO as RealQueueIO
24from .registry import routine as routine
5+
6+
7+ @contextmanager
8+ def activate ():
9+ with RealQueueIO ().activate ():
10+ yield
Load diff This file was deleted.
Original file line number Diff line number Diff line change 55from collections .abc import Iterable
66from concurrent .futures import Future
77from contextlib import contextmanager
8+ from contextvars import ContextVar
89from pathlib import Path
10+ from typing import Self
911
1012from .broker import Broker
1113from .consumer import Consumer
2426
2527
2628class QueueIO :
29+ __active = ContextVar [Self | None ]("active" , default = None )
30+
31+ @classmethod
32+ def active (cls ) -> Self :
33+ """Find the currently active instance."""
34+ queueio = cls .__active .get ()
35+ assert queueio is not None , "No active QueueIO instance"
36+ return queueio
37+
2738 def __init__ (
2839 self ,
2940 * ,
@@ -35,6 +46,16 @@ def __init__(
3546 self .__invocations = dict [Invocation , Message ]()
3647 self .__register_routines ()
3748
49+ @contextmanager
50+ def activate (self ):
51+ token = self .__active .set (self )
52+ try :
53+ with self .invocation_handler ():
54+ yield
55+ finally :
56+ self .__active .reset (token )
57+ self .shutdown ()
58+
3859 def __pyproject (self ) -> Path | None :
3960 for path in [cwd := Path .cwd (), * cwd .parents ]:
4061 candidate = path / "pyproject.toml"
Original file line number Diff line number Diff line change 22
33from time import sleep as time_sleep
44
5- from queueio import QueueIO
5+ from queueio import activate
66from queueio import routine
77from queueio .gather import gather
88from queueio .sleep import sleep
@@ -24,8 +24,5 @@ async def yielding(iterations: int):
2424
2525
2626if __name__ == "__main__" :
27- q = QueueIO ()
28- try :
29- q .submit (yielding (7 ))
30- finally :
31- q .shutdown ()
27+ with activate ():
28+ yielding (7 ).start ()
Original file line number Diff line number Diff line change 33
44import pytest
55
6- from queueio import QueueIO
76from queueio .invocation import Invocation
7+ from queueio .queueio import QueueIO
88
99from .basic import yielding
1010
1313def test_integration ():
1414 queueio = QueueIO ()
1515
16- try :
16+ with queueio . activate () :
1717 queueio .purge (queue = "queueio" )
1818 events = queueio .subscribe ({Invocation .Completed })
1919 invocation = yielding (7 )
@@ -36,6 +36,3 @@ def test_integration():
3636 except subprocess .TimeoutExpired :
3737 proc .kill ()
3838 proc .wait ()
39-
40- finally :
41- queueio .shutdown ()
Original file line number Diff line number Diff line change 11from contextlib import suppress
22from time import sleep as time_sleep
33
4- from queueio import QueueIO
4+ from queueio import activate
55from queueio import routine
66from queueio .gather import gather
77from queueio .sleep import sleep
@@ -47,8 +47,5 @@ async def irregular():
4747
4848
4949if __name__ == "__main__" :
50- q = QueueIO ()
51- try :
52- q .submit (irregular ())
53- finally :
54- q .shutdown ()
50+ with activate ():
51+ irregular ().start ()
Original file line number Diff line number Diff line change 33
44import pytest
55
6- from queueio import QueueIO
76from queueio .invocation import Invocation
7+ from queueio .queueio import QueueIO
88
99from .expanded import irregular
1010
@@ -14,7 +14,7 @@ def test_integration():
1414 # Prefers a clean environment and queue
1515 queueio = QueueIO ()
1616
17- try :
17+ with queueio . activate () :
1818 queueio .purge (queue = "queueio" )
1919 events = queueio .subscribe ({Invocation .Completed })
2020 invocation = irregular ()
@@ -39,7 +39,3 @@ def test_integration():
3939 # Force kill if it doesn't terminate gracefully
4040 proc .kill ()
4141 proc .wait ()
42-
43- finally :
44- # Always clean up the queueio instance
45- queueio .shutdown ()
Load diff This file was deleted.
You can’t perform that action at this time.
0 commit comments