【问题标题】:GCP Cloud Functions for python output data file用于 python 输出数据文件的 GCP 云函数
【发布时间】:2020-05-07 00:45:20
【问题描述】:

我对 GCP 非常陌生,不确定 Cloud Functions 是否适合此问题。

  1. 我有一个 python 脚本,它使用 tweepy 调用 twitter api,并生成一个 csv 文件,其中包含该特定用户名的推文列表。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tweepy
import datetime
import csv

def fetchTweets(username):
  # credentials from https://apps.twitter.com/
  consumerKey = "" # hidden for security reasons
  consumerSecret = "" # hidden for security reasons
  accessToken = "" # hidden for security reasons
  accessTokenSecret = "" # hidden for security reasons

  auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
  auth.set_access_token(accessToken, accessTokenSecret)

  api = tweepy.API(auth)

  startDate = datetime.datetime(2019, 1, 1, 0, 0, 0)
  endDate =   datetime.datetime.now()
  print (endDate)

  tweets = []
  tmpTweets = api.user_timeline(username)

  for tweet in tmpTweets:
      if tweet.created_at < endDate and tweet.created_at > startDate:
          tweets.append(tweet)

  lastid = ""
  while (tmpTweets[-1].created_at > startDate and tmpTweets[-1].id != lastid):
      print("Last Tweet @", tmpTweets[-1].created_at, " - fetching some more")
      lastid = tmpTweets[-1].id
      tmpTweets = api.user_timeline(username, max_id = tmpTweets[-1].id)
      for tweet in tmpTweets:
          if tweet.created_at < endDate and tweet.created_at > startDate:
              tweets.append(tweet)

  # # for CSV

  #transform the tweepy tweets into a 2D array that will populate the csv   
  outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]

  #write the csv    
  with open('%s_tweets.csv' % username, 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(["id","created","text"])
    writer.writerows(outtweets)
  pass

  f = open('%s_tweets.csv' % username, "r")
  contents = f.read()
  return contents

fetchTweets('usernameofusertoretrieve') # this will be set manually in production
  1. 我想运行此脚本并通过 http 请求检索结果(作为 csv 文件或 return contents),例如使用 JavaScript。该脚本只需要每天运行一次。但是生成的数据 (csv) 应该可以根据需要提供。

因此我的问题是

一个。 GCP Cloud Functions 是完成这项工作的正确工具吗?还是这需要更广泛的东西,因此需要一个 GCP VM 实例?

b.需要对代码进行哪些更改才能使其在 GCP 上运行?

也感谢任何有关方向的帮助/建议。

【问题讨论】:

  • 这是一个相当广泛的问题。 Cloud Functions 提供了一个可扩展至 0 并满足 REST 请求的计算框架。 Cloud Functions 没有持久存储,因此必须使用 Cloud Storage 的数据库。一种可能性是将 Cloud Function 作为计划作业每天运行一次,这会导致 CSV 存储在 GCS 存储桶中,然后请求者将直接检索文件的内容。基本上,一个 Cloud Function 调用即可从 twitter 中检索您的数据并创建 GCS 文件,其他一切都只是检索该文件。
  • 非常感谢您的详细评论。它真的帮助了我。我做了更多阅读并得出了使用 GCS 存储桶的相同解决方案。

标签: python google-cloud-platform google-cloud-functions tweepy twitterapi-python


【解决方案1】:

如果没有更多详细信息,您的问题很难回答。但是,我会尝试提供一些见解

GCP Cloud Functions 是适合这项工作的正确工具吗?还是这需要更广泛的东西,因此需要一个 GCP VM 实例?

这取决于。使用 1 个 CPU,您的处理时间是否会少于 9 分钟?您的进程是否会占用少于 2Gb 的内存(应用程序内存占用 + 文件大小 + tweets 数组大小)?

为什么是文件大小?因为只有/tmp 目录是可写的并且它是一个内存文件系统。

如果您需要长达 15 分钟的超时,您可以查看 Cloud Run,与 Cloud Function 和 I personally prefer 非常相似。 Cloud Function 和 Cloud Run 的 CPU 和内存限制相同(但在 2020 年应该会改变,CPU 和内存更多)

需要对代码进行哪些更改才能使其在 GCP 上运行?

首先从/tmp 目录写入和读取。最后,如果您希望您的文件全天可用,请将其存储在 Cloud Storage (https://cloud.google.com/storage/docs) 并在函数开始时检索它。如果不存在,则为当天生成,否则获取现有的。

然后,将函数def fetchTweets(username):的签名替换为def fetchTweets(request):,获取请求参数中的用户名

最后,如果你想要每天一代人,请设置Cloud Scheduler


你没有谈到安全。我建议您将您的功能部署在private mode

所以,这个答案中有很多 GCP 无服务器概念,我不知道你对 GCP 的了解。如果您想要某些部分的精度,请不要犹豫!

【讨论】:

  • 非常感谢您抽出宝贵时间做出如此详细的回复。我不认为该功能非常密集(在 CPU 或磁盘空间中),因为 twitter api 将数据限制为 3200 条推文。所以产生的文件只有几百kb。感谢您还指出了我完全忽略的安全方面。我能够实现一个 Cloud Scheduler 并授予它(仅它)调用 HTTP 触发器的权限。如果您可以突出显示更好的方法,请随时详细说明您的答案。就像我说的,我是初学者,对 GCP 很陌生。
  • 你可以看看这个答案:stackoverflow.com/questions/59825183/…
猜你喜欢
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 2021-02-13
  • 2021-09-12
  • 2021-03-30
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多