私はflock()
を使用しているPHPスクリプトを使用して、すでにスクリプトが実行中の場合は複数のインスタンスを拒否します。phpスクリプト - 入力を別のインスタンスにリダイレクト
スクリプトコールで提供される引数が、それらを処理できる既存のプロセスに転送されることを希望します。
すなわち:
test.phpを:
#!/usr/bin/env php
<?php
$lock_file = fopen (getcwd() . '/'. basename(__FILE__, '.php') . '.pid', 'c');
$got_lock = flock ($lock_file, LOCK_EX | LOCK_NB, $wouldblock);
if ($lock_file === false || (! $got_lock && ! $wouldblock)) :
throw new Exception ("Error opening or locking lock file");
error_log("execption thrown");
elseif (! $got_lock && $wouldblock) :
exit ("Another instance is already running; terminating.\n");
endif;
while (true) :
$input = $argv; // or incomming datas from other script ?
unset($argv);
if (is_array($input)) :
foreach ($input as $a) :
echo $a;
endforeach;
else :
echo $input;
endif;
endwhile;
?>
さて、私が実行した場合:
php -f test.php arg1 arg2
php -f test.php arg3 arg4
2番目の呼び出しにも出たが、私arg3とarg4がmaiにパイプされるようにしたいnプロセス。
どうすればいいですか?
IPCを最初に見ていますが、これはわかりませんでした。そうでなければ、SQLのアプローチは良い解決策に見えますが、同時アクセスの問題があまりにも多すぎると思われません。ありがとう! – Khorwin
@Khorwinだから、私はSQLiteを普通のテキストファイルなどと言いました。 SQLiteエンジンは、書き込み/読み込み/削除を同時に行う、実質的に無限のプロセス間での同期処理を行います。競合状態が発生せず、偶発的な上書きもなく、すべてのロックが必要ですSQLite。 – hanshenrik
おかげで、ありがとう@hanshenrik! – Khorwin