PHP implementation based on file lock to solve multiple processes simultaneously read and write a file

Source: Internet
Author: User
Tags flock
This article mainly introduces PHP based on file lock to solve multiple processes at the same time read and write a file, combined with examples of PHP use flock for file read and write lock operation usage, the need for friends can refer to the next

Specific as follows:

First of all, PHP is to support the process and does not support multithreading (this first clear), if it is for the file operation, in fact, you just need to add a lock to the file can be resolved, do not need other operations, PHP flock has helped you get it done.

With flock before writing the file lock, and so on after the completion of the lock, so that the implementation of multi-threaded and read and write a file to avoid conflict. It's probably the next process.

/**flock (File,lock,block) *file required, which specifies that an open file to be locked or released *lock required. Specifies which type of lock to use. *block is optional. If set to 1 or true, the other process is blocked when the lock is made. *lock*lock_sh to obtain a shared lock (read program) *LOCK_EX to obtain an exclusive lock (written program) *lock_un to release the lock (whether shared or exclusive) *LOCK_NB if you do not want flock () to Clog/*if (flock ($ FILE,LOCK_EX) {fwrite ($file, ' write more words '); Flock ($file, lock_un);} else{//Handling Error Logic}fclose ($file);)

Related recommendations:

PHP uses file locks to resolve high concurrency steps

Talking about PHP file lock

php file lock resolves high concurrency

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.