Yijin
Yijin

Reputation: 97

Do multiple casperjs instances share same cookies

I am a little confused with how multiple casperjs instances work simultaneously.

My understanding is if we have "casperjs" c.1.js, c.2.js, ..., c.x.js (they have same code) then it will create multiple processes and they should manage resources individually, like separate cookie files. If we just have "casperjs" c.x.js file multiple times, it will share same cookie file.

Is my understanding right?

Thanks for any input.

Upvotes: 1

Views: 231

Answers (1)

user4535610
user4535610

Reputation:

Each phantomjs instance has its own object phantom.cookies, if you run casperjs c.x.js multiple times, each instance will have its own cookies, if you want to store these cookies separately, you can use such bash script:

#!/bin/bash
# run it, e.g.: ./test.sh 10 snap.js  // 10 times snap.js
  export PHANTOMJS_EXECUTABLE=/tmp/casperjs/phantomjs        # ln -sf /tmp/casperjs/phantomjs /usr/local/bin
# export SLIMERJS_EXECUTABLE="/root/slimerjs-0.9.5/slimerjs" # ln -sf /root/slimerjs-0.9.5/slimerjs /usr/local/bin
num=0
while [ "$num" != "$1"  ]
do
let "num++"
echo instance_"$num" >>/root/t
/tmp/casperjs/bin/casperjs --cookies-file=/root/casperjs/cookies_"$num".txt /root/casperjs/"$2" >>/root/t &
echo "$num $1 $2"
done
exit 0

By doing so, you will have several workers that will use cookie separately.

SlimerJS:

Cookies are stored in a sqlite database in the mozilla profile. If you want to have persistent cookies, you cannot indicate a file like for PhantomJS, but you should create a permanent profile. See profiles.

Read also:
https://docs.slimerjs.org/current/api/cookie.html#cookie
https://docs.slimerjs.org/current/api/phantom.html#phantom-cookies

Upvotes: 1

Related Questions