Skip to content

Instantly share code, notes, and snippets.

@DavidMertz
Created July 21, 2019 19:24
Show Gist options
  • Save DavidMertz/70426a91b2b8efd67ebf813e105c1255 to your computer and use it in GitHub Desktop.
Save DavidMertz/70426a91b2b8efd67ebf813e105c1255 to your computer and use it in GitHub Desktop.
Python loops that might terminate
(base) 530-tmp % python thread_kill.py
[1, 2, 3, 4, 5, 1]
[1, 2, 3, 4, 5, 1, 2]
[1, 2, 3, 4, 5, 1, 2, 3]
[1, 2, 3, 4, 5, 1, 2, 3, 4]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4]
import threading
from time import sleep
items = []
def killer():
global items
sleep(0.00008)
for _ in range(1_000_000):
items[:] = []
threading.Thread(target=killer).start()
items = [1,2,3,4,5]
for item in items:
items.append(item)
print(items, flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment