Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- import Queue
- import socket
- import threading
- # We'll pickle a list of numbers, yet again:
- someList = [ 1, 2, 7, 9, 0 ]
- pickledList = pickle.dumps ( someList )
- # A revised version of our thread class:
- class ClientThread ( threading.Thread ):
- # Note that we do not override Thread's __init__ method.
- # The Queue module makes this not necessary.
- def run ( self ):
- # Have our thread serve "forever":
- while True:
- # Get a client out of the queue
- client = clientPool.get()
- # Check if we actually have an actual client in the client variable:
- if client != None:
- print 'Received connection:', client [ 1 ] [ 0 ]
- client [ 0 ].send ( pickledList )
- for x in xrange ( 10 ):
- print client [ 0 ].recv ( 1024 )
- client [ 0 ].close()
- print 'Closed connection:', client [ 1 ] [ 0 ]
- # Create our Queue:
- clientPool = Queue.Queue ( 0 )
- # Start two threads:
- for x in xrange ( 2 ):
- ClientThread().start()
- # Set up the server:
- server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
- server.bind ( ( '', 2727 ) )
- server.listen ( 5 )
- # Have the server serve "forever":
- while True:
- clientPool.put ( server.accept() )
- from threading import Thread
- class t ( Thread ):
- def run(self):
- print "(from thread) ",
- print i
- for i in range(1, 50):
- print i
- t().start()
- ClientThread(arg1, arg2, kwarg1="three times!").start()
- run(arg1, arg2, kwarg1="three times!")
- myThread.setMyAttribute('new value')
- run(self):
- localVar = globalVar # only for immutable types
- localList = globalList[:] # copy of a list
- localDict = globalDict.copy() # Warning! Shallow copy only!
Add Comment
Please, Sign In to add comment