Speed ​​up this (main) node.js server

I have a (stupid) node.js server, which is here for the sole purpose of calling a function from the data that it passed with each request, and responding to the result of that function. Here is the code I'm using:

var pageDown = require('./PageDown/Markdown.Sanitizer').getSanitizingConverter(), http = require('http'); http.createServer(function (req, res) { var data = ""; res.writeHead(200, {'Content-Type': 'text/plain'}); req.on('data', function (chunk) { data += chunk; }); req.on('end', function() { res.end(pageDown.makeHtml(data)); }); }).listen(1337, '127.0.0.1'); console.log('HServer running at http://127.0.0.1:1337/'); 

I am using this server from python with the following code (atm I just compare, so this is just a stress test):

 #!/usr/bin/env python # -*- coding: utf-8 -*- import requests for i in range(1, 100000): r = requests.post('http://localhost:1337/', data="lol") print i print "I'm done :')" 

My problem is that this method of success is slow. I have a large database that needs to be processed using this javascript function, and I am looking for ways to make this process faster. Therefore, suggestions are welcome!

+4
source share
2 answers

If all you do is invoke the node process to clear your HTML code, why not just do it in python? I assume that the markdown converter you are pointing to is the following: http://code.google.com/p/pagedown/source/browse/Markdown.Sanitizer.js . If so, why not convert it to python (or find similar python code) and work with it in the process. Nuts seem to make a network call to just start this process, as it will be much slower than just doing everything in python. Here are some python markdown tools I could find: http://packages.python.org/Markdown/ , https://github.com/trentm/python-markdown2 .

+2
source

You are not using the asynchronous design of node.js. In fact, what you do, IMHO, is an anti-pattern.

If you need to do the calculations in node.js than the server code is correct. But to use asynchronous architecture, you must put multiple instances of node.js servers behind a load balancer. You can even run these instances on the same machine to use multiple cores.

And in order to allow the client to profit from this many cases, you must also make your mail calls asynchronously. To do this, use some async http client instead of requests, for example http://www.tornadoweb.org/documentation/httpclient.html

It allows you to perform calculations on node.js instances in parallel.

+4
source

Source: https://habr.com/ru/post/1435339/


All Articles