Php multi-threaded operations on the same file-to be continued,

Source: Internet
Author: User
Tags flock

Php multi-threaded operations on the same file-to be continued,

The problem with agreeing to the file operation consent is that the logic is not suitable in some places. If multiple threads write data at the same time, it may lead to unsatisfactory results without locking. For the sake of security, and dirty reads (Database words) should use exclusive locks, which means that each operation can only be performed by one thread. Other threads can only wait. If the thread times out, an error is returned.

1 key function: 2 fopen () // open a file or an http address to form a file stream 3 flock () // lock the file and unlock 4 fwrite () // perform read/write operations on the file 5 fclose () // close the file stream

 

1 fopen () resource fopen (string $ filename, string $ mode [, bool $ use_include_path = false [, resource $ context]) 2 3 File Open mode: 4 'r': open the file in read-only mode and point the file pointer to the file header. Open in 'r + 'read/write mode and point the file pointer to the file header. Open in 6 'W' writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. Open in 'W + 'read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. 8 'A' write mode to open, point the file pointer to the end of the file. If the file does not exist, try to create it. 9 'a + 'Open the read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. 10'x' is created and opened in writing mode. The file Pointer Points to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call. 11 'x + 'is created and opened in read/write mode. Other actions are the same as those of 'x.

 

1 flock () bool flock (resource $ handle, int $ operation [, int & $ wouldblock]) 2 3 4 handle 5 file system pointer, which is typically composed of fopen () resource ). 6 operation 7 operation can be one of the following values: 8 bytes LOCK_SH get share lock (read program ). 9 ◦ LOCK_EX gets an exclusive lock (written Program. 10 slave LOCK_UN release lock (whether shared or exclusive ). 11 12 13 if you do not want the flock () to be blocked at the lock time, It is LOCK_NB (not supported on Windows). In other words, when a lock is not waiting, an error is reported and EWOULDBLOCK14 is returned, which is written as follows: $ re = flock (lockfd, LOCK_EX | LOCK_NB) 15 returns $ re =-1, errno = EWOULDBLOCK

 

1 fwrite () int fwrite (resource $ handle, string $ string [, int $ length]) 2 3 handle 4 file system pointer, which is typically composed of fopen () resource ). 5 string 6 The string that is to be written. 7 length 8 If length is specified, the write will stop after the length byte is written or the string is written. 9 10 note that if the length parameter is provided, the magic_quotes_runtime configuration option will be ignored, and the diagonal lines in the string will not be extracted. 11 12 fwrite () returns the number of written characters. If an error occurs, FALSE is returned.

 

1 fclose () bool fclose (resource $ handle) 2 3 handle4 file pointer must be valid and opened successfully through fopen () or fsockopen.

Normally, this does not fundamentally solve the blocking problem. It can only be mitigated. It is best to arrange requests in a queue through middleware or reids to ensure that the request queue will not lose data, however, it is not so important to write data. You can use this method to write data directly, and the data will be lost.

During preliminary debugging, you can use this method to directly write data in a file, which is more visual. You can use mongodb to replace the file or write multiple file categories into directories, to control the blocking problem, you need to use redis to control and ensure that requests are not lost while operating on a single file.

 

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.