@@ -128,121 +128,3 @@ print("Manager has been shut down.")
128128## License
129129
130130This project is licensed under the MIT License - see the ` LICENSE ` file for details.
131- ```
132-
133- ---
134-
135- ### `demo.py`
136-
137- ```python
138- import time
139- from gopy import go, new_channel
140-
141- def number_producer(ch):
142- """A producer "goroutine"."""
143- print("Producer starting...")
144- for i in range(5):
145- message = f"message {i}"
146- print(f"-> Sending: {message}")
147- ch << message
148- time.sleep(0.5)
149- ch.close()
150- print("Producer finished.")
151-
152- def cpu_task():
153- """A CPU-bound task."""
154- print("CPU task starting...")
155- result = sum(i * i for i in range(10_000_000))
156- print("CPU task finished.")
157- return result
158-
159- def main():
160- # --- Channel Demo ---
161- ch = new_channel()
162- go(number_producer, ch)
163-
164- print("Consumer waiting for messages...")
165- for received_message in ch:
166- print(f"<- Received: {received_message}")
167- print("Consumer finished.")
168-
169- print("\n" + "-"*20 + "\n")
170-
171- # --- CPU-Bound Demo ---
172- future = go(cpu_task, process=True)
173- print("Main thread continues while CPU task runs...")
174-
175- time.sleep(1)
176- print("Main thread did some other work.")
177-
178- result = future.result()
179- print(f"Got result from CPU task: {result}")
180-
181-
182- if __name__ == "__main__":
183- main()
184- ```
185-
186- ---
187-
188- ### ` tests/test_app.py `
189-
190- ``` python
191- import time
192- import pytest
193- import asyncio
194- from gopy import GoroutineManager
195-
196- def cpu_intensive_task (x ):
197- return sum (i * i for i in range (x))
198-
199- async def async_io_task (duration ):
200- await asyncio.sleep(duration)
201- return f " Slept for { duration} "
202-
203- def sync_io_task (duration ):
204- time.sleep(duration)
205- return f " Slept for { duration} "
206-
207-
208- def test_manager_lifecycle ():
209- """ Tests that the manager starts and shuts down cleanly."""
210- manager = GoroutineManager()
211- assert not manager._is_running
212- with manager as app:
213- assert app._is_running
214- future = app.go(sync_io_task, 0.01 )
215- assert future.result() == " Slept for 0.01"
216- assert not manager._is_running
217-
218- @pytest.fixture (scope = " module" )
219- def app_manager ():
220- """ A shared manager for all tests in this module."""
221- with GoroutineManager() as manager:
222- yield manager
223-
224- def test_go_with_sync_function (app_manager ):
225- future = app_manager.go(sync_io_task, 0.1 )
226- assert future.result(timeout = 1 ) == " Slept for 0.1"
227-
228- def test_go_with_async_function (app_manager ):
229- future = app_manager.go(async_io_task, 0.1 )
230- assert future.result(timeout = 1 ) == " Slept for 0.1"
231-
232- def test_go_with_multiprocessing (app_manager ):
233- future = app_manager.go(cpu_intensive_task, 1000 , process = True )
234- assert future.result(timeout = 5 ) == 332833500
235-
236- def test_channels_with_operators (app_manager ):
237- channel = app_manager.new_channel()
238-
239- def producer ():
240- time.sleep(0.1 )
241- channel << " hello"
242- channel.close()
243-
244- app_manager.go(producer)
245-
246- # Test iteration
247- items = [item for item in channel]
248- assert items == [" hello" ]
0 commit comments