flock

PHP大文件分片上传的实现方法

﹥>﹥吖頭↗ 提交于 2019-12-11 15:24:46
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、前言 在网站开发中,经常会有上传文件的需求,有的文件size太大直接上传,经常会导致上传过程中耗时太久,大量占用带宽资源,因此有了分片上传。 分片上传主要是前端将一个较大的文件分成等分的几片,标识当前分片是第几片和总共几片,待所有的分片均上传成功的时候,在后台进行合成文件即可。 二、开发过程中遇到的问题 分片的时候每片该分多大size?太大会出现“413 request entity too large” 分片上传的时候并不是严格按照分片的序号顺序上传,如何判断所有的分片均上传成功? 合成文件的时候如何判断保证合成一个完整的文件而不出错?多个分片同时上传的时候,读写文件没有独占锁的时候会导致合成错误。 三、问题解决 当出现413的时候,修改了 nginx.conf 和php.ini (1)nginx中添加client_max_body_size和client_body_buffer_size (2)php.ini添加post_max_size 和 upload_max_filesize 重启nginx和php-fpm 代码逻辑梳理和分享 (1)先获取当前分片是第几片以及总共几片 (2)创建一个文件夹用来存储所有的分片以及合成的文件 (3)变量$done初始为true,用来判断是否所有的分片都上传完成

PHP issues using flock - file locking

流过昼夜 提交于 2019-12-11 11:08:43
问题 I'm having a problem using the PHP flock() function. I need to write two different variables ( $O and $o ) but often it doesn't write the second variable ( $o ), maybe because the file is written two times in a row. Here's the code: include_once "changevar.php"; changevar("O",$seguimedia,$filename,0); changevar("o",$offerta,$filename,0); $seguimedia , $filename and $offerta are correctly set. changevar.php: function changevar($varname,$newval,$filename,$type) { while(!$fp=fopen($filename,"c+"

Does file() lock the file when reading?

◇◆丶佛笑我妖孽 提交于 2019-12-10 19:11:07
问题 I'm using file() to read through a file like an array with tabs. I want to lock the file but I cannot seem to get flock() working on the file. Is it possible to do this? If so, how? If not, does the file() lock the file from the start and alleviate any potential sharing problems? 回答1: According to the documentation (specifically the comments), it won't read a file that was locked via flock . You have 2 alternatives. Read the file with fgets (without checks for errors): $f = fopen($file, 'r');

How can I ensure only one copy of a Perl script is running at a time?

吃可爱长大的小学妹 提交于 2019-12-10 15:51:11
问题 I need to ensure that only one copy of my Perl script is running at a time. According to the suggestions here I wrote a sub to do the check: sub check_instances { open my $fh, '<', $0 or die $!; unless (flock($fh, LOCK_EX|LOCK_NB)) { print "$0 is already running. Exiting.\n"; exit 1; } } But it doesn't work. What can be the issue? 回答1: You're using a lexical filehandle scoped inside the sub. When check_instances returns, the filehandle is automatically closed, which releases the lock. So you

Does python's fcntl.flock function provide thread level locking of file access?

浪子不回头ぞ 提交于 2019-12-10 10:46:20
问题 Python's fcnt module provides a method called [flock][1] to proved file locking. It's description reads: Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().) Looking up the linux man page for flock, it only refers to cross process locking, for example: A call to flock() may block if an incompatible lock is held by another process

Running python script with cron only if not running

柔情痞子 提交于 2019-12-08 22:53:36
问题 I need to run a python script (job.py) every minute. This script must not be started if it is already running. Its execution time can be between 10 seconds and several hours. So I put into my crontab: * * * * * root cd /home/lorenzo/cron && python -u job.py 1>> /var/log/job/log 2>> /var/log/job/err To avoid starting the script when it is already running, I use flock(). This is the script (job.py): import fcntl import time import sys def doIncrediblyImportantThings (): for i in range (100):

PHP - Restrict cron job overlap with flock()

南笙酒味 提交于 2019-12-08 13:16:35
I have a php script that processes and creates lots of images which is being run every 5 minutes using cron job. I want to be able to limit this so it can only run once at a time and not overlap if each run takes longer than 5 minutes. flock() seems like the best way to achieve this but i am struggling to understand how exactly i should add this into my existing script. My cron job is setup to run the following file - images.php: $array=array("Volvo","BMW","Toyota","Audi","Ford","Alfa","Porsche","Mercedes"); foreach ($array as $car) { generateImageCustomFunction($car); } I want to use a non

PHP flock() - what's under the hood?

北战南征 提交于 2019-12-07 02:05:23
问题 After wrestling with PHP source for a half an hour, I gave up. :P The question is - what system call does the PHP flock() function call boil down to on a Gentoo Linux system? I'm having some issues with it (like block-for-30-seconds-in-every-one-of-20-loop-iterations kind of issues) and I would like to know why that is so. 回答1: // example: $stream = fopen( FILE , 'rb') or die('???'); $md = stream_get_meta_data($stream); echo $md['wrapper_type']; flock($stream); if this prints plainfile then

Does python's fcntl.flock function provide thread level locking of file access?

这一生的挚爱 提交于 2019-12-06 11:49:50
Python's fcnt module provides a method called [flock][1] to proved file locking. It's description reads: Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().) Looking up the linux man page for flock, it only refers to cross process locking, for example: A call to flock() may block if an incompatible lock is held by another process. To make a non-blocking request, include LOCK_NB (by ORing) with any of the above operations. So my

How do I atomically create a locked file in Linux?

半世苍凉 提交于 2019-12-05 16:50:48
问题 Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading the file, block until it is finished downloading. I've been trying to find the easiest way to do to this. The obvious way is to: create file w/ an exclusive lock active on it only if it doesn't exist (O_CREAT | O_EXCL) if file exists already: open file and acquire exclusive lock else: download to newly created file