Python threading blues

Some Python fan please tell me that I’m missing something.

Is this really the boilerplate necessary for creating even the simplest thread in Python?

import threading

class MyThread(threading.Thread):
  def __init__(self, arg, **kwargs):
    threading.Thread.__init__(self, **kwargs)
    self.arg = arg

  def run(self):
    print "I'm running in a thread, with arg %d!" % (self.arg)

thread = MyThread(5)
thread.start()

This is making me miss Ruby, for which the equivalent is:

thread = Thread.new(5) { |arg|
  puts "I'm running in a thread, with arg #{arg}!"
}

P.S. Gazelle 0.2 is making a lot of progress, but unfortunately won’t hit the 1 month mark I hoped for. Surprise surprise. But when it does come, it’s going to be awesome.

This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Python threading blues

  1. mayowa says:

    There is an easier way yet…

    import threading
    threading.Thread(target=your_function).start()

  2. josh says:

    Fantastic, just what I wanted to see. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>