PDA

View Full Version : multithreaded server


bestworldweb
11-22-2002, 08:49 PM
Can someone please post an example of a multithread server using sockets, not socketserver.

inkedmn
11-22-2002, 09:05 PM
check the docs, duder

http://www.python.org/doc/current/lib/module-threading.html

bestworldweb
11-22-2002, 09:07 PM
I have read all that, threading and sockets, but I still dont know how to do it. I just need to see a simple example

inkedmn
11-22-2002, 09:10 PM
you don't have any idea how to work with any of it?

assuming the above statement is false, what IN PARTICULAR are you having trouble getting?

[edit]
and there aren't really any "simple examples" of multithreaded servers because that's not exactly a basic network programming concept.

Strike
11-22-2002, 09:11 PM
http://www.twistedmatrix.com/

gufmn
11-22-2002, 09:13 PM
Hi, my name is gufmn :)

bestworldweb
11-22-2002, 09:47 PM
i know how to make a one socket server. I dont know how to use threads with a server. I know how to use sockets, threads and threading, but I dont know how to use them together, I also know what twisted is, but dont want to use it.

Strike
11-22-2002, 10:37 PM
Originally posted by bestworldweb
i know how to make a one socket server. I dont know how to use threads with a server. I know how to use sockets, threads and threading, but I dont know how to use them together, I also know what twisted is, but dont want to use it.
Please don't inundate us with information about what you are going to use this for or why you have dismissed the help we have given you, that would only encourage us to help you further.

bestworldweb
11-22-2002, 10:50 PM
Guys please, all I do is want to learn how to use sockets in python. Part of learning sockets is learning how to make a multi-threaded server but I cant find an example. If none of you can do this then please stop asking me questions 'cause I am just repeating myself :-)

bestworldweb
11-22-2002, 11:16 PM
Well, I have through trial and error created one, maybe it will be of some use to others. It listens on a random port:


#! /usr/bin/python
import socket,thread,random

def connection(client):
client.send("wassup")
client.close()
return 0

port=random.randrange(2000,3000)
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(("localhost",port))
server.listen(5)
print "Port :"+str(port)+"\n"
client,address=server.accept()
thread.start_new_thread(connection,(client,))

while 1:
client,address=server.accept()
thread.start_new_thread(connection,(client,))

jemfinch
11-23-2002, 02:08 AM
Ah, trial and error. The true way to really becoming a good coder.

I'm serious.

You see, when all you do is read someone else's example and tweak it until it works for your purposes, you're not learning how to code -- you're learning how to modify others' code (a useful skill, but not as useful as being able to code yourself). Don't inhibit your growth as a programmer by always asking for examples before trying things out yourself. Instead, try things out, and when it doesn't work and you can't figure out why, ask questions.

As a note, I've written tons of socket-based servers and clients, and I've never needed to write a threaded one. Take a serious look at any of the various asynchronous networking toolkits (asyncore, twisted, etc.) available for Python. They're the high-performance, simpler (no locks or anything needed) option.

Jeremy

skidooer
11-24-2002, 12:05 AM
This doesn't help you, but you should read this: http://www.kuro5hin.org/story/2002/11/18/22112/860

bestworldweb
11-24-2002, 06:00 AM
Very interesting article.