简体   繁体   中英

Do i need Node.js in Python like I would with PHP?

I have been using PHP for some time now. And I have been thinking about learning Node.js to go along with it to use the non blocking idea for creating an online game or app. There is quite a bit of info on using the two together. Using Node as part of the back end of a game could really speed up some aspects of the game, especially if the game allows users to play against each other in real time.

Well, lately I have also been looking into learning Python (yes I have a lot of time on my hands). There are many features about it over PHP which I really like. But for the use of Node.js to do the background work like I was considering with PHP, I cannot find much information at all. I have also noticed that Python does have some threading features. As I am still very new to Python's world, would I even need Node.js in Python? Can Python handle these kind of features that Node.js can? Or would there still be benefits to using Node, or would I actually need it.

As a side note, since I started looking up Python, I also discovered Twisted which seems to be another framework like Node. But Twisted is written in Python. So in either of the cases above would Twisted be better (aside from the fact that Twisted seems to have been out longer and is more stable than Node). I just mean in general is it worth using at all, either Node or Twisted? And if so is one better than the other with Python?

Sorry for the big question, but I'm just so unsure and n00b in this area. Thanks.


So as it stands, after reading the helpful answers, I see the following options:

  1. PHP + JS
  2. Python + Twisted
  3. Python + PyJamas
  4. Python + Node.js
  5. Node.js
  6. Twisted

  1. I already know PHP and am comfortable with it, and am currently learning JS. This this was a major possible route for me. But I was also leaning away from PHP to Python because the in general features of the language I liked.

  2. This option I thought might be more plausible than #3, using Twisted to handle the networking port to allow the player to play live with each other.

  3. This just makes it so you don't have to learn JS, which to me doesn't seem like that big of a deal. I've already started studying it and it's not that hard to learn. But like was mentioned in a question, mixing things like ; and {} could potentially have some issues.

  4. Like #2, but with Node.js. Mostly I see the addition of Node to handle the networking aspect to let the players be able to play in a live/real-time game. And the majority of the code would be in Python.

  5. Sole Node.js was a consideration as well as it is the single language route. But it doesn't have the same benefits of learning and using Python either (it being a general scripting language I can use in, and out of web design. A big reason I wanted to learn and use it.).

  6. Ans as #5 but I was not considering an only Twisted route until reading the comments. While it seems plausible, it doesn't really involve one of the two languages I want to learn, Python and Node.

The above seems to be the main routes I can go. Now I'm still not really sure which route to go. I really want to learn both Node and Python. So it seems I may just need to learn the two separately. But I still need to pick a choice for this project. And so far I am liking numbers 2 and 5, with 6 a close to 5 since Node and Twisted have some very similar functionality. And 1 as a mention because that's what I already know. But I was wanting to learn something new anyways. So still, really numbers 2 and 5. Or 4 as it's similar to 2. Hah, I still need to do some homework. Maybe it deserves another question.

EDIT (9-19-2012): I just wanted to update, to say that I am using mostly Node.js currently for development. And plan on using Redis for PubSub functionality to give the appearance of real time page updates, as I do not need true real time as in games, or in paired content editing.

While Python can definitely be used for Asynchronous Programming, it doesn't feel natural, even with Twisted, if you compare it to Node.js it just doesn't look or feel that nice.

Since you're planing on doing a real-time Web Game, you'll most likely will end up using WebSockets .

WebSockets are based on HTTP and use the upgrade header to initiate the bi-directional connection, that means, that you can easily have both your normal Server and your WebSockets run on port 80, if you need a lot of fall backs to support older Browsers, then there's always the almighty Socket.IO .

Depending on how complicated your front-end will be I'd rather go with express.js or just write my own stuff.

Having both the front-end and the game in the same process has (obviously) a lot of advantages, you can fetch a lot of information without the need of having to query the database.

Another big "feature" is, that you don't have to context switch between the client logic, and the server's logic. That might seems like a small benefit at first, but besides the fact that you won't type ; in Python and don't forget your {} in JS after having worked continuously on either side for a couple of hours, you will also be able to re-use code between Server and Client. Again that might look like a small benefit at first, but good multi player games always run a lot of stuff on the client, just to compensate the lag, also Python and JavaScript are quite different in place so having to re-write portions of JS in Python takes time and may even introduce bugs.

(Now on to the shameless plugs...)

I've done 2 multi player games with Node.js already, although the have no HTTP front end both games run quite a lot of JS on the Client:
Multiplayer Asteroids/Geometry Wars Crossover
RTS Concept (a bit like Eufloria)

Also, while JSON seems to fit perfectly for sending the data between Browser and client, you'll soon find out that it uses a ton of bandwidth, since I encountered the same problem I've written some specialized library which saves up to 45% traffic:
BiSON.js

Again, having JavaScript both on the Server and the Client lets one re-use code and therefore save development time.

So to sum it all up I would strongly suggest to go with Node.js:

  1. Re-usable code, less context switching therefore shorter development time
  2. V8 is faster than Python in many cases.
  3. No concurrency issues, everything is async by default.
  4. Node.js is the next big thing, jump on the bandwagon now.
  5. It's JavaScript! ;)

I don't think it's so much that it's better because it's Python-on-Python, but because you can do both the game part and the web part in Twisted.

EDIT:

Also, Pyjamas .

If you like callback-oriented programming, twisted and nodejs are the thing for you. Otherwise, you could take a look at gevent . It is similar to twisted/nodejs in that it is an asynchronous framework, but it allows you to write code just like you would do in a threaded approach.

It achieves this by doing coroutines-based magic behind the scenes.

The whole point of using Node.js is its strengths which is well documented at http://nodejs.org/#about . While you can certainly use a server-side language and a frontend stack for your needs, I think writing all code in 1 language will be a huge productivity boost.

if i were you, I'll attempt to write most of my code in 1 language as much as possible. So I do not think you should try to use Node.js together with Python (Twisted or Tornado) . It seems to have some kind of overlap.

Just imagine the coolness of writing all of your code in JavaScript. ;)

It sounds to me like you're talking about having a system in place to do some kind of processing in the background that you want to do asynchronously. If that's the case you might consider using some kind of queueing system. This way you can put a message into the queue until it gets processed by a pool of worker processes.

Celery makes this pretty easy to do, but getting RabbitMQ (or another message broker) configured correctly might be a bit of a pain if you haven't done that before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM