Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Last active October 28, 2023 04:57
Show Gist options
  • Save tonybaloney/387b602320dbc4d1c9bff4343e0882fe to your computer and use it in GitHub Desktop.
Save tonybaloney/387b602320dbc4d1c9bff4343e0882fe to your computer and use it in GitHub Desktop.

Talk notes

Links

Code samples

These are the code samples used in the talk

Challenges with threading

import numpy
# Create a random array of 100,000 integers between 0 and 100
a = numpy.random.randint(0, 100, 100_000)
for x in a:
  abs(x - 50)

Benchmark comparison -

import numpy
import threading
# Create a random array of 100,000 integers between 0 and 100
a = numpy.random.randint(0, 100, 100_000)
def simple_abs_range(vec):
  for x in vec:
    abs(x - 50)
def f_linear():
  # Calculate the distance for each value to 50
  simple_abs_range(a)
def f_threaded():
  threads = []
  # Split array into blocks of 100 and start a thread for each
  for ar in numpy.split(a, 100):
    t = threading.Thread(target=simple_abs_range, args=(ar,))
    t.start()
    threads.append(t)
  for t in threads:
    t.join()

How do I use sub interpreters in 3.12?

import _xxsubinterpreters as interpreters
interpreters.run('''
print("Hello World")
''')

Non-blocking example:

from threading import Thread
import _xxsubinterpreters as interpreters
t = Thread(target=interpreters.run, args=("print('hello world')",))
t.start()

How might interpreters work in 3.13?

import interpreters

def f():
  print('hello world’)
  import things
  things.do_stuff()
interpreters.run(f)
def f(x):
  ...

with interpreters.PoolExecutor(interpreters=4) as pool:
  # Map a sequence of calls to interpreters
  pool.map(f, [1, 2, 3, ...])
  # or run a single task
  pool.submit(f, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment