Created
October 9, 2025 15:29
-
-
Save bencord0/240ae1df9daac501c238dd9d19ba4d35 to your computer and use it in GitHub Desktop.
Multicore Python using concurrent.interpreters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from concurrent import interpreters | |
| interps = [ | |
| interpreters.create(), | |
| interpreters.create(), | |
| interpreters.create(), | |
| ] | |
| def fib(n): | |
| a, b = 0, 1 | |
| for _ in range(n): | |
| a, b = b, a + b | |
| def submain(): | |
| interp = interpreters.get_current() | |
| ident = interp.id | |
| print(f'Starting interpreter: {ident}') | |
| fib(ident * 999999) | |
| def main(): | |
| threads = [] | |
| for interp in interps: | |
| t = interp.call_in_thread(submain) | |
| threads.append(t) | |
| print('All spawned') | |
| for t in threads: | |
| t.join() | |
| print(f'{t.name} Joined') | |
| print('All joined') | |
| for interp in interps: | |
| interp.close() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment