Spaces:
Sleeping
Sleeping
| import multiprocessing | |
| import os | |
| import time | |
| def worker(result_queue): | |
| print(f"Child process pid {os.getpid()} starting...") | |
| try: | |
| result_queue.put(f"Success from pid {os.getpid()}") | |
| except Exception as e: | |
| result_queue.put(f"Error: {e}") | |
| if __name__ == "__main__": | |
| # Explicitly use 'spawn' as it is the default on Windows | |
| # and what's causing the issue with nested functions. | |
| multiprocessing.set_start_method('spawn', force=True) | |
| q = multiprocessing.Queue() | |
| p = multiprocessing.Process(target=worker, args=(q,)) | |
| print(f"Parent process pid {os.getpid()} starting child...") | |
| p.start() | |
| p.join(timeout=10) | |
| if p.is_alive(): | |
| print("Process timed out!") | |
| p.terminate() | |
| p.join() | |
| elif not q.empty(): | |
| print(f"Result: {q.get()}") | |
| else: | |
| print("No result in queue after process finished") | |
| q.close() | |
| try: | |
| q.join_thread() | |
| except: pass | |
| print("Test finished.") | |