PHP flock()非阻塞仍然阻塞为什么?(PHP flock() non-blocking still block why?)
我使用flock()函数通过获取临时文件上的锁来检查脚本的另一个实例是否已在运行,因此下一个实例应该检查文件是否未锁定,否则它会停止
<?php $fp = fopen("/var/tmp/your.lock", "w"); if (!flock($fp, LOCK_EX|LOCK_NB)) { // try to get exclusive lock, non-blocking die("Another instance is running"); } //my script sleep(10); echo 'completed successfully';
当从同一浏览器同时打开两个实例时,该脚本在不同浏览器同时调用两次文件时工作正常,即第一次调用获取锁定,第二次调用等待锁定而不关闭
我知道可能有其他方法来检查一个实例文件是否已经正常工作,但其中大多数会做一件事情然后撤消它,在我的用例中,脚本可能随时结束,因为它可能需要很长时间或超过内存限制或无论如何
任何帮助?
I'm using flock() function to check if another instance of the script is already running by obtaining the lock on a temporary file so next instance should check if the file is not locked otherwise it stops
<?php $fp = fopen("/var/tmp/your.lock", "w"); if (!flock($fp, LOCK_EX|LOCK_NB)) { // try to get exclusive lock, non-blocking die("Another instance is running"); } //my script sleep(10); echo 'completed successfully';
the script works without problem when calling the file twice at the same time from different browsers while it waits if I opened two instances at the same time from the same browser i.e the first call get the lock and the second wait for the lock and not closing
I know there may be other ways to check if a file an instance is already working but most of them will do a thing then undo it and in my use case the script may end any time as it may take long time or exceed memory limit or by any reason
any help ?
原文:https://stackoverflow.com/questions/13331809
最满意答案
你可以使用以下(progromatically)
ed.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); ed.setKeyListener(DigitsKeyListener.getInstance("0123456789.$"));
您可以在
DigitsKeyListener.getInstance
定义所需的任何字符或者如果不希望用户输入$符号并且您希望在用户完成编辑后手动输入...
@Override protected OnFocusChangeListener getOnFocusChangeListener() { return new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { //Member variable in the class which contains an EditText CurrencyTextbox.this.hadFocus = true; } else { // Thes EditText has lost focus Log.v(TAG, "Lost focus."); if (CurrencyTextbox.this.hadFocus) { // We previously had focus, now we lost it, format the user input! // Get current value of the Textbox String value = CurrencyTextbox.this.textbox.getText().toString(); // Formatting the user input value = String.format(//Doing some formatting); // Reset the had focus CurrencyTextbox.this.hadFocus = false; } } } };
you can use the following (progromatically)
ed.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); ed.setKeyListener(DigitsKeyListener.getInstance("0123456789.$"));
you can define whatever characters you want within the
DigitsKeyListener.getInstance
or if dont want the user to enter the $ sign and you want to be entered manually after the user finishes editting...
@Override protected OnFocusChangeListener getOnFocusChangeListener() { return new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { //Member variable in the class which contains an EditText CurrencyTextbox.this.hadFocus = true; } else { // Thes EditText has lost focus Log.v(TAG, "Lost focus."); if (CurrencyTextbox.this.hadFocus) { // We previously had focus, now we lost it, format the user input! // Get current value of the Textbox String value = CurrencyTextbox.this.textbox.getText().toString(); // Formatting the user input value = String.format(//Doing some formatting); // Reset the had focus CurrencyTextbox.this.hadFocus = false; } } } };
相关问答
更多-
请将这两行添加到您的EditText XML文件中,它可以工作: android:inputType="textMultiLine|number" android:digits="0,1,2,3,4,5,6,7,8,9" Please add these two lines to your EditText XML file, it works: android:inputType="textMultiLine|number" android:digits="0,1,2,3,4,5,6,7,8,9"
-
InputType用于向IME提示它应显示的模式(例如,电子邮件地址条目,数字条目,字母表条目)。 即使IME模式没有提供输入无效字符的方法,用户仍然可以将任何可能的字符粘贴到EditText中。 您需要使用的是InputFilter,用于在输入每个字符时清理EditText的值。 InputType is used to hint to the IME which mode it should display as (e.g. email address entry, number entry, alph ...
-
您可以使用eclipse中的属性选项卡设置各种值。 这里是所有可能的值 没有 文本 textCapCharacters textCapWords textCapSentences textAutoCorrect textAutoComplete textMultiLine textImeMultiLine textNoSuggestions textUri textEmailAddress textEmailSubject textShortMessage textLongMessage textPerso ...
-
尝试这样的事情: if(txt.getInputType()==InputType.TYPE_CLASS_PHONE){ //do something } 还要检查这个问题: Android:使用InputMethodManager 从它描述的代码中确定活动输入法 ** 另一种方法: ** 在开发自定义键盘时:如此处所述http://developer.android.com/guide/topics/text/creating-input-method.html#IMEUI 你扩展了InputMetho ...
-
改用输入类型 input.setInputType(InputType.TYPE_CLASS_NUMBER); use just input type instead input.setInputType(InputType.TYPE_CLASS_NUMBER);
-
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" android:inputType="textCapCharacters" android:capitalize="sentences" 使用.xml文件中的上述行,以便它只输入字母和数字 android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ...
-
我不认为有办法提供这样的任意过滤器。 您必须使用提供的过滤器类型 。 如果找不到那些符合要求的类型的组合,则可能需要使用EditText上的TextWatcher或InputFilter 自行进行过滤。 I don't think there is a way to provide an arbitrary filter like that. You have to use the provided filter types. If you can't find a combination of those ...
-
EditText inputType特殊情况只获取数字和特殊符号(EditText inputType special case to get only number and a special symbol)[2023-06-17]
你可以使用以下(progromatically) ed.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); ed.setKeyListener(DigitsKeyListener.getInstance("0123456789.$")); 您可以在DigitsKeyListener.getInstance定义所需的任何字符 或者如果不希望用户输入$符号并且您希望在用户完成编辑后手动输入... @Override protected OnFocusChange ... -
此问题的解决方案是将所有edittext设置在LinearLayout中,并将该可见性设置为该LinearLayout上的“Gone”而不是EditText。 这样,您仍然可以通过单击任何其他元素以编程方式关注EditText。 The solution to this problem is to set all the edittext in in a LinearLayout and to set visibility to "Gone" on that LinearLayout and not on ...
-
在EditText中使用inputType:numberDecimal进行可能的验证(Possible validations in EditText with inputType:numberDecimal)[2022-11-02]
如果您不处理任何浮动, TextUtils.isDigitsOnly() ( javadoc )可能会有所帮助。 If you don't handle any floats, TextUtils.isDigitsOnly() (javadoc) might be helpful.