Threads in Python

python

A thread, sometimes called an execution context or a lightweight process, is a single sequential flow of control within a program. You use threads to isolate tasks. Each thread is a sequential flow of control within the same program. Each thread runs independently from the others, but at the same time.

We want to run a certain function a specified number of times per second. A class should be given such a function and some parameters, like the maximum number of threads, the number of active threads per second and a total duration.

import threading, time

class Fire:

   def soldier(self, soldier_nr):

      while (1):

         self.work_lock.acquire()

         if (self.still_working):

            self.write_lock.acquire()
            self.nr_active_soldiers += 1
            self.write_lock.release()

            start_time_counter = self.time_counter

            self.function.__call__(self.arguments)

            end_time_counter = self.time_counter

            self.write_lock.acquire()
            self.nr_active_soldiers -= 1
            self.statistics.append((soldier_nr, start_time_counter, end_time_counter))
            self.write_lock.release()

         else:

            break;

   def __init__(self, function, arguments, nr_threads, per_second, duration):

       self.function           = function
       self.arguments          = arguments
       self.nr_threads         = nr_threads
       self.per_second         = per_second
       self.duration           = duration
       self.work_lock          = threading.Semaphore(0)
       self.write_lock         = threading.Semaphore(1)
       self.still_working      = 1
       self.nr_active_soldiers = 0
       self.time_counter       = 0
       self.statistics         = []

   def fire(self):

       print 'Start'

       for i in range(self.nr_threads):
          thread1 = threading.Thread(target=self.soldier, args=(i, ))
          thread1.start()

       for second in range(self.duration):
          for i in range(self.per_second):
             self.work_lock.release()
          self.write_lock.acquire()
          if (len(self.statistics) > 0):
             (soldier_nr, start_time_counter, end_time_counter) = self.statistics[-1]
             delta_time = end_time_counter - start_time_counter
          else:
             delta_time = -1
          print 'Active', self.nr_active_soldiers, 'Time', delta_time
          self.write_lock.release()
          time.sleep(1)
          self.time_counter += 1

       self.still_working = 0
       for i in range(self.nr_threads):
          self.work_lock.release()

       time.sleep(5)

       output_file = open('output.txt', 'wb')

       for statistic in self.statistics:
           output_file.write(str(statistic) + 'n')

       output_file.close()

       print 'End'

if (__name__ == '__main__'):

   def waste_time(arguments):

       time.sleep(2.5)

   nr_threads = 20
   per_second = 3
   duration   = 15

   firing_squad = Fire(waste_time, None, nr_threads, per_second, duration)
   firing_squad.fire()
Advertisements
This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s