multithreading - Client Server programming in python? -


Here is the source code for the client in multithreaed server and python.

The code closes the client and the server closes after the job ends, to avoid the overhead of keeping the connections alive and on the same connection and opening the sockets at all times Want to send more data than

The following code is from:

  import pickle import socket import threading # We will pick up a list of numbers: someList = [1, 2, 7, 9, 0] PickledList = pickle.dumps (someList) # Our thread class: Class ThreadFread (Threading Thread): To accept the requirements of # parameter, __int_ method of the # override thread: def __init__ (auto, channel, description): self.channel = Channel self.details = details threading. Thread .__ init__ (self) Def run (self): Print 'Connection received:', self description [0] xrange (10) for x self.channel.send (pickledList): print self.channel.recv (1024 ) Self.channel.close () Print 'Close Connection:', Self.details [0] # Set up the server: server = socket.socket (socket.acf.meet, socket.SOCK_STREAM) server.b server (", 2727) ) Server.listen (5) # The server has been provided "Forever": While true: Channel, Description = server.accept () ClientTrade (channel, description) .start ()  Import import socket Import threading # Here is our thread: Class Connection Thru D (threading. Thread): def run (manually): connect to server # client: client = socket.socket (Socket.AF_INET, socket.SOCK_STREAM) client.connect (('localhost', 2727)) # List objects recovered and Unplicare: print pickle.loads (client.recv (1024)) Send some messages: xrange for x (10): client.send ('hex' + str (x) + '\ n') # connection client Closed () # generate some thread: xrange (5) in x: connectionthread (). Start ()  

Creating a new bag for each connection is very bad What is the design option if you get many connections?

Basically, waiting for the thread to network IO is not worth it. Your program becomes really complex and you do not make no profit wait fast since you wait for the network in the thread, Using it.

The following text is from Python documentation:

There are only two ways to keep a program on a single processor "More than one thing at a time." Multi-threaded programming The simplest and most popular way to do it, but there is another very different technique, which can give you almost all the advantages of multi-threading, in fact many threads. It's really only practical if your program is roughly tied to I / O. If your programmaker is compelled, then pre-forwarded Scheduled Threads probably what you really want though, network servers are less processor bound, though.

And if this is a processor bound server case, you can always leave another process / thread to the processor part. Continuous:

If your operating system supports selected system calls in your I / O library (and almost all do), then you can juggle many communication channels at once Can use it for; While your I / O "background" is happening in other works. Although this strategy may seem weird and complex, especially before, it is easy to understand and control in many ways than multi-threaded programming.

Therefore, instead of using threads, use non-blocking input / output: Collect the socket in the list and use an event loop to find out the data for reading in the socket is. Do it in a thread.

You can choose a dragon asynchronous networking structure like this for you. With this you will get many headaches, the code of twisted has been improved for many years, and some corner cases are covered, for whom you will take master on time.

Edit : There is an existing async IO library (like Twisted) Python code, you could write it yourself, but it has been written for you already. I do not know why you will not use any one of those libraries and instead will write your own worst code, because you are getting started Netwearing IO is difficult to find right.


Comments