Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69377 Topics- by 58433 Members - Latest Member: graysonsolis

April 29, 2024, 01:10:55 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Multi-Threaded Python Servers?
Pages: [1]
Print
Author Topic: Multi-Threaded Python Servers?  (Read 1874 times)
Cranktrain
Level 4
****


making gams


View Profile WWW
« on: March 19, 2009, 02:45:48 AM »

Hi, (new here :D )

I'm a Python programmer, been playing around with it for half a year now, mainly building small game servers using the Twisted Matrix framework and more recently, the python implementation of the OINC protocol. What fun! I'm mainly wondering how important writing a Multi-Threaded game server is? Let's assume that the game is somewhere between slow turn-based and super-fast-fps type game, and that all logic is performed server-side to keep it nice and secure. Add in the fact that my latest experiment deals with player accounts on an SQLite database, and we can go back to my original question, is a multi-threaded server really important?

My initial tests with 3 or 4 people on the server, quite predictably, didn't slow down my server a jot, but if I was to boost that up to 30 or even 100 players, I might run into problems, right?

I've never ever even touched multi-threadedness in any language, let alone Python, and I've heard it can be quite tricky. But hey, even if I manage to get the network part of it on one thread and the database-ing stuff on another, that must speed things up right?

Responses, advice and sighs of disbelief from fellow Python programmers more than welcome.
« Last Edit: March 19, 2009, 09:12:46 PM by Derek » Logged

george
Level 7
**



View Profile
« Reply #1 on: March 19, 2009, 07:35:34 AM »

You might want to investigate EVE Online -- maybe an overblown example for your purposes, but I think they use Stackless Python on their server -- http://www.slideshare.net/Arbow/stackless-python-in-eve
« Last Edit: March 19, 2009, 07:05:16 PM by george » Logged
cpets
Level 0
**


View Profile WWW
« Reply #2 on: March 19, 2009, 08:16:06 AM »

I second the stackless recommendation, it's designed exactly for this sort of thing.

Keep in mind also that python threads are not OS threads, so if you're looking to take advantage of multiple cores or anything they won't help you out.

That said, if you're writing your server within some framework, the framework will often take care of running different instances of your application in different processes. I guess you're not responding to http requests, but if you were, something like django running under apache takes care of all the threading/multiprocess concerns. If you use a well-established database layer like django's or sqlalchemy, you don't, up to a certain scale, have to worry abou concurrent database access at all.

But of course the most important consideration is, until you see a problem, you probably don't need to worry about it. It's possible that your game will not fail on a single-threaded server until you start seeing hundreds of concurrent games, so I'd definitely write up some tests to simulate that situation before you try new approaches.
Logged
Cranktrain
Level 4
****


making gams


View Profile WWW
« Reply #3 on: March 19, 2009, 10:54:19 AM »

Thanks for the replies, I'm rather interested in how Eve uses Python in their servers. I'll certainly look into using Django for databases and database management, I've played with it before.

I think I'll continue developing as I am now, but keeping an eye on certain function times to see how they fair.
Logged

mcc
Level 10
*****


glitch


View Profile WWW
« Reply #4 on: March 19, 2009, 05:32:49 PM »

Is it in fact possible to use Django and Stackless Python at the same time?
Logged

My projects:<br />Games: Jumpman Retro-futuristic platforming iJumpman iPhone version Drumcircle PC+smartphone music toy<br />More: RUN HELLO
bateleur
Level 10
*****



View Profile
« Reply #5 on: March 20, 2009, 01:24:08 AM »

I've never ever even touched multi-threadedness in any language, let alone Python, and I've heard it can be quite tricky.

It can be tricky, but most of the really horrible problems are caused by people designing their apps badly.

In a nutshell: don't have 100 threads using a lock to mediate access to some resource when you could give exclusive access to the resource to one thread and have the others pass requests to it.
Logged

Hooray! The end.
Guest
« Reply #6 on: April 10, 2009, 03:38:05 PM »

Quote
The most important consideration is, until you see a problem, you probably don't need to worry about it. It's possible that your game will not fail on a single-threaded server until you start seeing hundreds of concurrent games, so I'd definitely write up some tests to simulate that situation before you try new approaches.

I second this.  Threads aren't always all that helpful in languages like Python anyway.  The fastest Python web server right now is single-threaded.

Stackless Python is definitely something to look into, but I think you are best off sticking with Twisted for now.  The whole point of that framework is to allow its users to efficiently and concurrently handle multiple connections without needing multiple threads.  (It has pretty nice thread support anyway, if you end up needing that.)
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic