我在 Google App Engine 上成功部署了一個 Twitter 截圖機器人。這是我第一次部署。
我注意到的第一件事是,在我單擊鏈接之前,該應用程式沒有開始運行。當我這樣做時,只要選項卡正在加載和打開,該應用程式就會成功運行(回復帶有螢屏截圖的推文)。當我關閉選項卡時,機器人停止作業。
另外,在 cloud shell 日志中,我看到:
Handling signal: term
[INFO] Worker exiting (pid 18)
這種行為讓我感到驚訝,因為我希望它可以無限期地在谷歌服務器上運行。我的機器人通過 Twitter api 進行流式傳輸。上面的“工人退出”線也讓我感到驚訝。
以下是相關代碼:
def get_stream(set):
global servecount
with requests.get(f"https://api.twitter.com/2/tweets/search/stream?tweet.fields=id,author_id&user.fields=id,username&expansions=author_id,referenced_tweets.id", auth=bearer_oauth, stream=True) as response:
print(response.status_code)
if response.status_code == 429:
print(f"returned code 429, waiting for 60 seconds to try again")
print(response.text)
time.sleep(60)
return
if response.status_code != 200:
raise Exception(
f"Cannot get stream (HTTP {response.status_code}): {response.text}"
)
for response_line in response.iter_lines():
if response_line:
json_response = json.loads(response_line)
print(json.dumps(json_response, indent=4))
if json_response['data']['referenced_tweets'][0]['type'] != "replied_to":
print(f"that was a {json_response['data']['referenced_tweets'][0]['type']} tweet not a reply. Moving on.")
continue
uname = json_response['includes']['users'][0]['username']
tid = json_response['data']['id']
reply_tid = json_response['includes']['tweets'][0]['id']
or_uid = json_response['includes']['tweets'][0]['author_id']
print(uname, tid, reply_tid, or_uid)
followers = api.get_follower_ids(user_id='1509540822815055881')
uid = int(json_response['data']['author_id'])
if uid not in followers:
try:
client.create_tweet(text=f"{uname}, you need to follow me first :)\nPlease follow and retry. \n\n\nIf there is a problem, please speak with my creator, @JoIyke_", in_reply_to_tweet_id=tid, media_ids=[mid])
except:
print("tweet failed")
continue
mid = getmedia(uname, reply_tid)
#try:
client.create_tweet(text=f"{uname}, here is your screenshot: \n\n\nIf there is a problem, please speak with my creator, @JoIyke_", in_reply_to_tweet_id=tid, media_ids=[mid])
#print(f"served {servecount} users with screenshot")
#servecount = 1
#except:
# print("tweet failed")
editlogger()
def main():
servecount, tries = 1, 1
rules = get_rules()
delete = delete_all_rules(rules)
set = set_rules(delete)
while True:
print(f"starting try: {tries}")
get_stream(set)
tries = 1
如果這很重要,我的app.yaml檔案只有一行:
runtime: python38
我從云外殼部署了應用程式gcloud app deploy app.yaml
我能做些什么?我已經搜索過,似乎無法找到解決方案。另外,這是我第一次成功部署應用程式。謝謝你。
uj5u.com熱心網友回復:
Google App Engine 按需作業,即當它接收到 HTTP(s) 請求時。
預熱請求和min_instances > 0 都不能滿足您的需求。熱身會在您的請求進入之前嘗試“啟動”一個實體。 min_instance > 0 只是表示不要終止該實體,但您仍然需要一個 http 請求來呼叫該服務(這是您通過打開瀏覽器選項卡和輸入您的應用程式網址)。
您可能會問 - 既然您已經通過打開瀏覽器選項卡“啟動”了實體,為什么之后它不再繼續運行?答案是,對 Google App Engine(標準)應用程式的每個請求都必須在 1 到 10 分鐘內完成(取決于您的應用程式使用的擴展型別)(請參閱檔案)。對于 Google App Engine Flexible,超時時間最長為 60 分鐘。這告訴您,您的服務將在 GAE 標準最多 10 分鐘或 GAE Flexible 最多 60 分鐘后超時。
我認為在 GCP 上最適合您的解決方案是使用Google Compute Engine (GCE)。啟動虛擬服務器(選擇最低配置,以便您可以堅持免費層)。如果您使用 GCE,這意味著您啟動了一個虛擬機 (VM),將您的代碼部署到它并啟動您的代碼。然后您的代碼連續運行。
uj5u.com熱心網友回復:
App Engine 按需作業,即只有在應用程式有請求時才會啟動(這就是為什么當您單擊應用程式的 URL 時)。您還可以將 1 個實體設定為“一直運行”(min_instances),這將是您想要完成的任務和 App Engine 的反模式。請閱讀如何管理實體
查看您的代碼,您每分鐘都從Twitter提取資料,因此對您來說最好的選擇是使用Cloud Scheduler Cloud Functions。
Cloud Scheduler 將呼叫您的 Function 并檢查是否有要處理的資料,如果沒有則終止該行程。這將幫助您節省成本,因為該功能不會一直運行,而是只會在需要的時間內運行。
另一方面,我不是Twitter API方面的專家,但如果有一種方法可以代替從Twitter和Twitter中提取資料直接呼叫你的函式,那會更好,因為你可以優化成本并且函式只會運行當有資料要處理而不是每n分鐘檢查一次時。
作為建議,首先查看您在 GCP 中的所有選項或您將使用的提供程式,然后選擇最適合您的用例的選項。在這種情況下,僅選擇一種適用于您的編程語言的語言并不一定會像您期望的那樣作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461035.html
