[英]time.sleep in python does not work but exits immediately
I am using below code in Nifi in executeScript and have added time.sleep to add delay in the code so that it tries after some time but this does not work as expected. 我正在executeScript中的Nifi中使用以下代码,并添加了time.sleep以在代码中添加延迟,因此它会在一段时间后尝试,但此操作无法按预期工作。 It prints the logs one after the other without waiting
它一个接一个地打印日志,无需等待
class ModJSON(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
obj = json.loads(text)
response = self._updateelasticsearch(timestamp, flowID, elasticSearchURL, indexName)
log.error("response : " + response)
flowcounter = 1
while (response.find('"updated":1') == -1 & flowcounter < 35):
flowcounter += 1
time.sleep(50)
response = self._updateelasticsearch(timestamp, flowID, elasticSearchURL, indexName)
flowcounter = 0
outputStream.write(bytearray(json.dumps(response, indent=4).encode('utf-8')))
def _updateelasticsearch(self, timestamp, flowID, elasticSearchURL, indexName):
try:
#update code
return rest_response
def _validateIfExist(self, flowid, elasticSearchURL, indexName) :
#validatecode
if record_count > 0:
return True
else :
return False
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, ModJSON())
session.transfer(flowFile, REL_SUCCESS)
session.commit()
I suspect your while
loop is never being entered. 我怀疑您的
while
循环永远不会进入。
while (response.find('"updated":1') == -1 & flowcounter < 35):
^ problem
The bitwise &
operator behaves differently than the boolean and
operator. 按位
&
运算符的行为不同于布尔and
运算符。 &
has different precedence than and
, so it can give surprising results: &
具有不同的优先级比and
,所以它可以给令人吃惊的结果:
>>> 1 == 1 & 2 == 2
False
Similarly, your expression response.find('"updated":1') == -1 & flowcounter < 35
only evaluates to True when response.find('"updated":1') == (-1 & flowcounter)
and when (-1 & flowcounter) < 35
. 同样,表达式
response.find('"updated":1') == -1 & flowcounter < 35
仅在response.find('"updated":1') == (-1 & flowcounter)
和当(-1 & flowcounter) < 35
。 (-1 & flowcounter)
is never equal to -1 when flowcounter
is positive, so your conditional will never succeed when response.find('"updated":1')
returns -1. (-1 & flowcounter)
在flowcounter
为正数时永远不等于-1,因此当response.find('"updated":1')
返回-1时,您的条件将永远不会成功。
If you're only trying to logically chain together clauses in a conditional, use and
. 如果您只是想将条件中的子句逻辑地链接在一起,请使用
and
。
while (response.find('"updated":1') == -1 and flowcounter < 35):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.