使用无限循环进行网络连接是否被认为是一种好的编程实践?
示例:
//connect to server
conn = getConnection()
while True:
data = conn.getData()
//do something
我想以一种高效的方式来实现这一点,比如注册一个事件并使程序休眠。我正在寻找一种类似于在Linux中处理信号的实现。
我的目标语言是C、Python和Java。
下面的bash脚本存在简单的语法问题。我不知道用什么语法将if语句嵌套到while语句中,或者使用bash shell脚本(对于所有linux都是新的)是否可能:
#!/bin/bash
myCombo=$((RANDOM%99999+10000));
echo ${myCombo};
myCracker=00000;
while [ (($myCracker<=99999)) ]; do
if [ $myCracker -eq myCombo ]
then
echo "The combination is " ${myCracker
我正在尝试写一个bash脚本,询问一个人是否想要一杯茶。Y返回“太好了,我现在就去给控制台泡茶”;N在停止循环之前再返回4次“你确定”。此外,如果在4次后续报价中,用户改变主意,按‘Y’键,电脑会将“很好,我现在就泡茶”打印到控制台上。
下面的脚本会产生一个无限循环,所以我需要在代码中进行一些增量计数,而我只是不知道在哪里?这可能是一个非常基本的问题,如果是这样的话,对于Linux Bash脚本来说,任何帮助都值得赞赏:
#!bin/bash
#tea2.sh
echo "Will you have a cup of tea?"
read answer
while [ "
据我所知,当循环的条件段没有终止条件时,这个循环被称为无限循环。
例如,
while True:
text = input("Enter something (q to quit) # ")
print(text)
if text=="q":
break
或,如果循环从未停止,则称为无限循环。
例如,
i = 0
while i == 0:
text = input("Enter something (q to quit) # ")
print(text)
我说对了吗?
我试图使用以下代码解决问题:
def inmobi(t):
while t:
li = map(int, raw_input().split())
n, ans = li[0], li[1:]
one, two, op = [], [], []
for e in range(n):
for i in range(1, n):
if e + i < n:
one.append(ans[e] + ans[e + i])
我试着在我的局域网中设置了服务器的windows10机器上运行脚本。我想为脚本重新启动30秒后,它完成,永远。有没有比把它放在一个无限的while循环中更好的方法呢?
下面是我的代码:
import Time
while True:
print("Here does all the things it's supposed to")
# Does something
time.wait(30)
虽然这是有效的,但我担心它会影响服务器的性能和内存,或者随着时间的推移它会变慢。
有没有更好的方法在windows中永远循环运行一段代码,也许是用
我不习惯编写Bash脚本,Google也没有帮助找出这个脚本的错误所在:
#!/bin/bash
while read myline
do
done
echo "Hello"
while read line
do
done
exit 0
我得到的输出是:
./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'
我的bash版本是:
GNU bash, version 3.2.25(1)-release (i686-re
这是做作业用的。这所特殊的学校提供0的帮助,而教授则没有多大帮助。我只是在寻求关于为什么这段代码不起作用的指导。我必须使用Python2.7。当我运行程序时,它要求我输入适当数量的品脱,但之后什么也不做。
# This program finds the average number of pints collected, the highest amount, and the lowest amount
# Lab 9-4 Blood drive
#the main function
def main():
endProgram = 'no'
print
我今天用javascript浏览了一段代码,突然遇到了这样一种情况:有一段代码片段。
add: function (value) {
var current = null;
if (this._root === null) {
this._root = node;
} else {
current = this._root;
while (true) {
// Some code goes here.
}
}
}
条件while(true)将始终为true。它没有进入我的头
我开始在我的页面上随机获取502个错误。这和Azure或我的网站有关联吗?自周四以来,我没有做任何修改,所以我对作为我的代码很少怀疑。
502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) cont
我必须每秒钟调用php函数。
如果我将其放置在php文件中,则在调用用户时,它会运行几次每次调用php文件。
index.php
//code that i want run everysecond (or a few second)
$bitsqlz = "SELECT id FROM tasks where id ='$todoid' and state='0';";
$dataz = mysqli_query($con1,$bitsqlz);
while ($rowz= $dataz-&g
我试图在Java中实现快速排序算法。以下代码与我的大学教授提供的代码相同。它工作得很好,直到我将Object[] {1,2,3,14,24,5,454,1,24,2,11}设置为输入(显然左=0,右= 10)。这个算法似乎陷入了无限循环,但我找不到在哪里,也找不到原因。我已经看了几天了,我找不到这个错误。我研究了其他类似的问题与快速排序算法在网站上,但他们没有帮助解决我的问题。我会接受任何想法。
static int partition (Object[] a, int left, int right, Comparator c){
Object pivot = a[left];
我有一个arduino代码,它执行如下任务:
while (a=='A')
{
//do the task A
}
while (a=='B')
{
//do the task B
}
当它们单独运行时,这些循环可以正确运行。但当我尝试组合这两个循环时,问题就来了。
void loop ()
{
while (a=='A')
{
//do the task A
}
while (a=='B')
{
//do the task B
}
}
下面是代码的完整细节: