Casperjs web service multithread

I am using a CasperJS script as a web service, accessing it from a node server. What I did not manage to do is to make Casper be 'multithread'. If I make two simultaneously requests to Casper from postman the result will be something scrambled between both requests for one response, and the second will be null. I saw that PhantomJS has a page principle, but I did not find anything similar for Casper.

  • Can i call Casper's web service with multiple requests at the same time and get correct/coherent responses?
  • Is there some configuration needed for the web server to allow me to do this?
  • Should the request be done in a 'special manner'? Are there any caveats regarding this that i should be aware of?
  • If it can only function sequentially, would starting multiple servers on the same machine but different ports solve the issue?
  • Here is the casper web service i am talking about. When I make a request like locahost:1338/?query=name.name it will crawl for that query on the specified url. My problem comes when I make 2 parallel requests with different queries.

    
        //includes web server modules
        "use strict";
        var port = 1338;
        var server = require('webserver').create();
        var url = 'url to scrap';
    
        //start web server
        var service = server.listen(port, function(request, response) {
            var arr1 = [];
            var arr2 = [];
            var arr3 = [];
    
            var casper = require("casper").create({
                  verbose: true,
                  logLevel: 'error',
                  pageSettings: {
                    loadImages: false,
                    loadPlugins: false,
                    userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
                  },
                  clientScripts: ["vendor/jquery-1.12.1.js"]
            });
    
            casper.start(url, function() {
            }, function(){
                console.log(url + " not found");
                return;
            });
    
            casper.waitForSelector('.cssClass', function() {
            }, function(){
                console.log("not found");
                return;
            });
    
            casper.then(function() {
                var query = getQuery(request.url);
                casper.sendKeys('.cssClass', query);
                casper.click('.cssClass');
    
                casper.waitForSelector('.cssClass', function(){
                    arr1 = this.evaluate(function(){
                        var nodeList = document.querySelectorAll(".cssClass");
                        return Array.prototype.map.call(nodeList, function(node){
                            return node.textContent;
                        });
                    });
                }, function(){
                    console.log("not found");
                    return;
                });
    
                casper.then(function(){
                    if(names.length > 0)
                    {
    
                        casper.waitForSelector('.cssClass', function(){
                            arr2 = this.evaluate(function(){
                                var nodeList = document.querySelectorAll(".cssClass");
                                return Array.prototype.map.call(nodeList, function(node){
                                    return node.textContent;
                                });
                            });
                            console.log("found");
                        }, function(){
                            console.log("not found");
                            return;
                        });
    
                        casper.waitForSelector('.cssClass', function(){
                            arr3 = this.evaluate(function(){
                                var nodeList = document.querySelectorAll(".cssClass");
                                return Array.prototype.map.call(nodeList, function(node){
                                    return node.src;
                                });
                            });
                            console.log("found");
                        }, function(){
                            console.log("not found");
                            return;
                        });
                    }
                });
            });
    
            casper.run(function() {
                response.statusCode = 200;
                response.write(JSON.stringify({p1: arr1, p2: arr2, p3: arr3}));
                response.close();              
            });
        });
    
        console.log('nServer running at http://localhost:' + port+'/');
    
    
    链接地址: http://www.djcxy.com/p/73488.html

    上一篇: require()在CasperJS中失败

    下一篇: Casperjs web服务多线程