Guest User

Untitled

a guest
May 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import pickle
  2. import Queue
  3. import socket
  4. import threading
  5.  
  6. # We'll pickle a list of numbers, yet again:
  7. someList = [ 1, 2, 7, 9, 0 ]
  8. pickledList = pickle.dumps ( someList )
  9.  
  10. # A revised version of our thread class:
  11. class ClientThread ( threading.Thread ):
  12.  
  13. # Note that we do not override Thread's __init__ method.
  14. # The Queue module makes this not necessary.
  15.  
  16. def run ( self ):
  17.  
  18. # Have our thread serve "forever":
  19. while True:
  20.  
  21. # Get a client out of the queue
  22. client = clientPool.get()
  23.  
  24. # Check if we actually have an actual client in the client variable:
  25. if client != None:
  26.  
  27. print 'Received connection:', client [ 1 ] [ 0 ]
  28. client [ 0 ].send ( pickledList )
  29. for x in xrange ( 10 ):
  30. print client [ 0 ].recv ( 1024 )
  31. client [ 0 ].close()
  32. print 'Closed connection:', client [ 1 ] [ 0 ]
  33.  
  34. # Create our Queue:
  35. clientPool = Queue.Queue ( 0 )
  36.  
  37. # Start two threads:
  38. for x in xrange ( 2 ):
  39. ClientThread().start()
  40.  
  41. # Set up the server:
  42. server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  43. server.bind ( ( '', 2727 ) )
  44. server.listen ( 5 )
  45.  
  46. # Have the server serve "forever":
  47. while True:
  48. clientPool.put ( server.accept() )
  49.  
  50. from threading import Thread
  51. class t ( Thread ):
  52. def run(self):
  53. print "(from thread) ",
  54. print i
  55.  
  56. for i in range(1, 50):
  57. print i
  58. t().start()
  59.  
  60. ClientThread(arg1, arg2, kwarg1="three times!").start()
  61.  
  62. run(arg1, arg2, kwarg1="three times!")
  63.  
  64. myThread.setMyAttribute('new value')
  65.  
  66. run(self):
  67. localVar = globalVar # only for immutable types
  68. localList = globalList[:] # copy of a list
  69. localDict = globalDict.copy() # Warning! Shallow copy only!
Add Comment
Please, Sign In to add comment