如何使Firebase的Python脚本始终运行?

2024-04-25 23:47:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个Python脚本来截取新的Firebase文档,更新一个参数,然后继续监听。你知道吗

我希望它能保持24/7的运行,并不断地这样做。下面是我正在运行的代码:

import datetime
import time
from time import sleep
from google.cloud import firestore
import google.cloud.exceptions
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore 
from pprint import pprint
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="fbkey.json"

# Use a service account
cred = credentials.Certificate('fbkey.json')
firebase_admin.initialize_app(cred)

db = firestore.client()

def setAsReady(docId):
    ref = db.collection(u'tasks').document(docId)
    ref.update({u'status': 'READY'})

def on_snapshot(col_snapshot, changes, read_time):
    for doc in changes:
        newDocId = doc.document.id
        print(newDocId)
        setAsReady(newDocId)

col_query = db.collection(u'tasks').where(u'status', u'==', u'NEW')
query_watch = col_query.on_snapshot(on_snapshot)

time.sleep(500000000)

现在我使用time.sleep(5000000000)来保持脚本运行,不过,我认为这是个坏主意。你知道吗

我想在Ubuntu服务器上运行这个脚本,所以我把它放在screen窗口中,它运行了半个小时左右,不过后来我发现这个错误:

  File "/usr/local/lib/python3.5/dist-packages/grpc/_channel.py", line 358, in _next
    raise self
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.INTERNAL
        details = "Received RST_STREAM with error code 0"
        debug_error_string = "{"created":"@1565213741.785723473","description":"Error received from peer ipv4:172.217.14.202:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"Received RST_STREAM with error code 0","grpc_status":13}"
>
Exception in thread Thread-OnRpcTerminated:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.5/dist-packages/google/cloud/firestore_v1/watch.py", line 290, in close
    raise reason
google.api_core.exceptions.InternalServerError: 500 Received RST_STREAM with error code 0

Tags: infromimportselfadmintimelibusr