Next post: Physics into audio

Thread unsafety

I thought of a new way to "solve" the FizzBuzz problem... in a way that's incredibly inefficient - but unique.

#!/usr/bin/env python
import time, itertools
from threading import Thread

class FizzBuzz():
  g_place = ''
  def th(self, t, tminc, fn):
    start = time.clock()
    while True:
      t += tminc
      while time.clock() < start + t:
        time.sleep(0.01)
      self.g_place = fn(self.g_place)
    
  def go(self, counter, printer):
    thds = ((0.7, 1.0, lambda c: printer(c)), 
      (0.0, 1.0, lambda c: counter.next()),
      (0.1, 3.0, lambda c: 'Fizz'),
      (0.2, 5.0, lambda c: 'FizzBuzz' if c=='Fizz' else 'Buzz'))
    for thd in thds: Thread(target=self.th, args=thd).start()

def _print(s):
  print s

FizzBuzz().go(itertools.count(1), _print)
Don't use this in product code, for a variety of reasons. But at least it's multithreaded!