home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnd101.zip / Add-Ons / Fnorb / examples / threaded / client.py next >
Text File  |  1999-06-28  |  4KB  |  144 lines

  1. #!/usr/bin/env python
  2. #############################################################################
  3. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997
  4. # All Rights Reserved.
  5. #
  6. # The software contained on this media is the property of the DSTC Pty
  7. # Ltd.  Use of this software is strictly in accordance with the
  8. # license agreement in the accompanying LICENSE.HTML file.  If your
  9. # distribution of this software does not contain a LICENSE.HTML file
  10. # then you have no rights to use this software in any manner and
  11. # should contact DSTC at the address below to determine an appropriate
  12. # licensing arrangement.
  13. #      DSTC Pty Ltd
  14. #      Level 7, Gehrmann Labs
  15. #      University of Queensland
  16. #      St Lucia, 4072
  17. #      Australia
  18. #      Tel: +61 7 3365 4310
  19. #      Fax: +61 7 3365 4311
  20. #      Email: enquiries@dstc.edu.au
  21. # This software is being provided "AS IS" without warranty of any
  22. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  23. # kind arising out of or in connection with the use or performance of
  24. # this software.
  25. #
  26. # Project:      Distributed Environment
  27. # File:         $Source: /units/arch/src/Fnorb/examples/threaded/RCS/client.py,v $
  28. # Version:      @(#)$RCSfile: client.py,v $ $Revision: 1.1 $
  29. #
  30. #############################################################################
  31. """ Client for the multi-threaded example. """
  32.  
  33.  
  34. # Standard/built-in modules.
  35. import sys, time, thread
  36.  
  37. # Fnorb modules.
  38. from Fnorb.orb import CORBA
  39.  
  40. # Stubs generated by 'fnidl'.
  41. import HelloWorld
  42.  
  43.  
  44. class Client:
  45.  
  46.     def __init__(self, server, num_threads):
  47.     """ Constructor. """
  48.  
  49.     self.__server = server
  50.     self.__num_threads = num_threads
  51.     self.__lk = thread.allocate_lock()
  52.  
  53.     return
  54.  
  55.     def start(self):
  56.     """ Start the threads. """
  57.  
  58.     for i in range(self.__num_threads):
  59.         thread.start_new_thread(self.__worker_thread, (i,))
  60.  
  61.     return
  62.  
  63.     def wait(self):
  64.     """ Wait for all threads to complete. """
  65.  
  66.     self.__lk.acquire()
  67.     num_threads = self.__num_threads
  68.     self.__lk.release()
  69.  
  70.     while num_threads > 0:
  71.         time.sleep(2)
  72.  
  73.         self.__lk.acquire()
  74.         num_threads = self.__num_threads
  75.         self.__lk.release()
  76.  
  77.     return
  78.  
  79.     #########################################################################
  80.     # Private interface.
  81.     #########################################################################
  82.  
  83.     def __worker_thread(self, thread_no):
  84.     """ Worker thread. """
  85.  
  86.     print 'Thread:', thread_no, 'Started...'
  87.  
  88.     # Payload!
  89.     payload = '\0' * 16000
  90.  
  91.     # Time 100 calls!
  92.     start = time.time()
  93.     for i in range(100):
  94.         self.__server.hello_world(payload)
  95.     end = time.time()
  96.  
  97.     print 'Thread:', thread_no, 'Time taken:', end - start
  98.  
  99.     self.__lk.acquire()
  100.     self.__num_threads = self.__num_threads - 1
  101.     self.__lk.release()
  102.  
  103.     thread.exit()
  104.  
  105.  
  106. def main(argv):
  107.     """ Do it! """
  108.  
  109.     print 'Initialising the ORB...'
  110.  
  111.     # Initialise the ORB.
  112.     orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
  113.  
  114.     # Read the server's stringified IOR from a file (this is just a 'cheap and
  115.     # cheerful' way of locating the server - in practise the client would use
  116.     # the naming or trader services).
  117.     stringified_ior = open('server.ref', 'r').read()
  118.  
  119.     # Convert the stringified IOR into an active object reference.
  120.     server = orb.string_to_object(stringified_ior)
  121.  
  122.     # Create a client with 10 threads!
  123.     client = Client(server, 10)
  124.  
  125.     # Start the threads.
  126.     client.start()
  127.  
  128.     # Wait for them all to complete.
  129.     client.wait()
  130.  
  131.     print 'All threads complete!'
  132.  
  133.     return 0
  134.     
  135. #############################################################################
  136.  
  137. if __name__ == '__main__':
  138.     # Do it!
  139.     sys.exit(main(sys.argv))
  140.  
  141. #############################################################################
  142.