PHP Multi-threaded operation of the same file-to be continued

Source: Internet
Author: User
Tags flock

The problem with agreeing to file operation consent files is that logic is not appropriate in some places, if multiple threads are written at the same time, without locking, it can result in the result is unsatisfactory, in order to secure, and dirty read (the database word), should use an exclusive lock, which means that only one thread at a time to operate. Other threads can only wait, and if the timeout is an error.

1 key functions: 2 fopen ()  //Open file or an HTTP address to form a file stream 3flock()//File locking and Unlock 4fwrite  ()//For file read and write operations 5fclose()//close file stream

1 fopen()Resource fopen(string $filename,string $mode[, BOOL$use _include_path=false[,Resource $context ]] )2 3 File open mode:4' R 'read-only opens, pointing the file pointer to the file header. 5' R+ 'read-write mode opens, pointing the file pointer to the file header. 6' W 'The write method opens, pointing the file pointer to the file header and truncating the file size to zero.  If the file does not exist, try to create it. 7' w+ 'read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero.  If the file does not exist, try to create it. 8Awrite to open, pointing the file pointer to the end of the file.  If the file does not exist, try to create it. 9' A + 'read-write mode opens, pointing the file pointer to the end of the file.  If the file does not exist, try to create it. Ten' X ' is created and opened in writing, pointing the file pointer to the file header. If the file already exists, thefopen() call failed and returnedFALSE, and generates ae_warningLevel of error information. If the file does not exist, try to create it. This specifies o_excl| for the underlying open (2) system callthe o_creat tag is equivalent.  One' x+ ' is created and opened as read-write, and other behaviors are the same as ' X '.

1 Flock() boolFlock(Resource $handleInt$operation[, Int &$wouldblock ] )2 3 4 Handle5File system pointers, which are typicallyfopen() Created byResource(Resources). 6 Operation7 operation can be one of the following values:8 ?  Lock_sh Get a shared lock (read program). 9 ?  LOCK_EX obtains an exclusive lock (written program. Ten ?  Lock_un release Lock (whether shared or exclusive).  One  A  -If you do not wantFlock() blocked when locked, it is LOCK_NB (not supported on Windows), that is, encountered a lock does not wait, direct error returned Ewouldblock -The wording is:$re=Flock(lockfd,lock_ex|lock_nb) thereturn if wrong$re= -1,errno = Ewouldblock

1 fwrite() intfwrite(Resource $handle,string $string[, int$length ] )2 3 Handle4File system pointers, which are typicallyfopen() Created byResource(Resources). 5 string6ThestringThat's to be written.7 length8If length is specified, the length is written to a byte or thestringlater, the write will stop, depending on what kind of situation is encountered first. 9 TenNote If the length parameter is given, theMagic_quotes_runtimeThe configuration options are ignored, and thestringthe slash in will not be pumped out.  One  A fwrite() returns the number of characters written and returns when an error occursFALSE。

1 fclose ()   fcloseresource$handle  )23handle 4 fopen Fsockopen () successfully opened.

Usually this does not solve the blocking problem from the root, can only be mitigated, preferably through the middleware or reids the request into a queue, to ensure that the request queue does not lose data, but for the data written is not so important, you can use this method, direct write, loss is lost.

In the early debugging, you can use this method to write data directly in the file, the visualization of a strong, later can be used to write MongoDB replacement files, or multiple file classification sub-directory write, and control blocking problems need to use REDIS, etc. to control the single file operation, the request will not be lost.

PHP Multi-threaded operation of the same file-to be continued

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.