create new tag
, view all tags
Python threading is heavily utilised in this testbed in order to achieve concurrent processing. This is provided through the usage of the threading module. Threads are created within the class structure used to define the link, network and application layers.

Critical functions are defined as the main threads (that is, they must exit for the main program to terminate). All other non-essential functions (for example, congestion control, timers) are defined as daemon threads (that is, the main program can exit even if there are still daemon threads alive).

Graceful startup and termination of layers is achieved by defining various threading.Event() exit condition variables in the main program, which is then passed to each layer. When the testbed is set to exit, it will flag the corresponding exit event, which notifies the next layer that it should begin its termination sequence. In addition to this, functions have been defined at each layer in order to facilitate structured termination of the main threads associated at each layer.

A summary of the sequence of events is as follows:


  • 1. Start main program
  • 2. Start link layer
    • a. Start link layer threads
  • 3. Start network layer
    • a. Start network layer threads
  • 4. Start relevant application layer
    • a. Start relevant application layer threads
  • 5. Main program waits on its exit event


  • 1. Flag for termination
    • a. Set first layer’s exit event
  • 2. Application layer’s exit event is set
    • a. Terminate application layer’s main threads
    • b. Set next layer’s exit event
  • 3. Network layer’s exit event is set
    • a. Terminate network layer’s main threads
    • b. Set next layer’s exit event
  • 4. Link layer’s exit event is set
    • a. Terminate link layer’s main threads
    • b. Set main program’s exit event
  • 5. Main program’s exit event is set
  • 6. Proceed to run remaining functions

Note that because of modularity, the startup and termination sequence of layers can be performed in any order. The order above was picked because it made logical sense. Because of the heavy use of threading, a number of thread safe variables were used to manage the flow of the testbed across all the relevant components. These include Queue.Queue(), threading.Event() and threading.Lock(). In particular, ‘tokens’ are defined through threading.Lock() for variables or resources that may be accessed and changed by more than one thread at any time. A thread must obtain the associated variable token before it can gain access to it, and this is done neatly in Python with the with function. Tokens are mainly used to manage access to stdout (stdout_token), the log file (writelog_token), and various counters.

Use of these functions is as follows:

  • with self.stdout_token: print ‘…’
  • with self.writelog_token: WriteLog(…)
Topic revision: r2 - 2012-01-18 - XiaohongWu
This site is powered by the TWiki collaboration platformCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback