Ajax-implemented long polling does not block other AJAX requests for pages within the same time (same domain request)

Source: Internet
Author: User

Recently to do a call to the screen function, using the OM system interface, OM system send the request to the interface program, analysis data added to the MySQL database, and then the latest data ID with the total number of calls today to Memcache. The screen program is based on the data in the read memcache, comparing whether there is a new request coming. In the middle of the problem is: the front desk when polling waiting for data, the page of other requests blocked, looked up a lot of information, including the Apache MPM mode, have not found the reason. Later in the forum to find the reason: the following

In fact, it is not possible to access the same site using the session of the page, because access to a page, the session is locked, b page to wait for a page end release lock to be executed. The solution is to not use the session, or the session immediately after the release (PHP use session_write_close release session lock).

Here is the code for my specific operation:

Js:

    functionLoad_cdr_data () {//get the current data        varUrl= ' __module__/cdrmanage/get_cdr_info?id= '; varCdr_count = $ (' input[name= "Cdr_count"]). Val (); $.ajax ({type:"POST", DataType:"JSON", Async:true, URL:' __module__/cdrmanage/get_newcdr ', timeout:80000,//Ajax Request time-out 80 secondsdata: {time:80, Count:cdr_count,},//after 40 seconds, the data is returned regardless of the result serverSuccessfunction(data) {//get data from the server, display the data, and continue querying                if(Data.status = = "10") {                    varTitle= ' The phone you just dialed. ‘; URL=url+data.cdr_id; Open_no_savebtn_dialog (‘‘, Url,title); $(' Input[name= ' Cdr_count "] '). Val (Data.count);                New_load_cdr_data (); }                //not getting data from the server, continue to query over time                if(Data.status = = "20") {new_load_cdr_data (); }            },            //Ajax Request timed out, continue queryingErrorfunction(XMLHttpRequest, Textstatus, Errorthrown) {if(Textstatus = = "Timeout") {new_load_cdr_data ();    }            }        }); }

PHP Code implementation

    /** * Get CDR data information when initial load does not return data at first time the count data is 0 * @access Public*/     Public functionGet_newcdr () {Import (' Vendor.phpmemcache.Memcachemanage '); $precount= I (' Post.count '); $time= I (' Post.time '); $user _id= Session (' user_id '); $host= ' 127.0.0.1 '; $port= ' 11211 '; $expire= ' 0 '; $memcache _prefix= ' '; $mem=New\memcachemanage ($host,$port,$expire,$memcache _prefix); if(Empty($time)) {            Set_time_limit(0); }        //Infinite Request Timeout is permanently executed until the end of the program//session unlock this place for a long time to see//For security reasons (avoid session data confusion caused by individual user concurrent requests),//php uses exclusive Lock to mutually exclusive access session data//(whether you are using session file or Memcache session),//That is, after opening the session, a single user can only serial access to this site,//concurrent requests only one will be immediately answered, others will be blocked        Session_write_close();//exclusive locking mechanism due to session occupancy        $i= 0;  while(true) {            $i++; //If the data is obtained, return the data to the customer service immediately and end the request//operation Memcache get the data.            $CDR _info=$mem->get ($user _id); $max _id=$CDR _info[' max_id ']; $count=$CDR _info[' Count ']; if($count>$precount) {                //You can return a URL and then popup the form implementation                $return=Array(' status ' = ' + ', ' cdr_id ' =$max _id, ' count ' =$count); Exit(Json_encode ($return)); }            if($i==$time) {                //prevents the program from executing the return data until the front desk closes and stops the request                $return=Array(' status ' = ' 20 '); Exit(Json_encode ($return)); }            Usleep(500000);//1 seconds for a second to determine the status of the data        }    }

Ajax-implemented long polling does not block other AJAX requests for pages within the same time (same domain request)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.