【发布时间】:2021-04-28 09:35:38
【问题描述】:
我有一个 Python 函数并将其部署在 Azure Funtion 上。我用了一个TXT文件来保存时间,我会用它。 在本地,此功能运行没有问题。但我在 Azure 上运行时收到以下错误:
Result: Failure Exception: OSError: [Errno 30] Read-only file system: 'date_time.txt' Stack:
在我的搜索中,我找到了一些类似 TempFile 的解决方案,但我下次需要 date_time.txt 的值。
我的代码:
import os
import psycopg2
import datetime
import logging
import tempfile
import pandas as pd
import json
import sqlalchemy
from azure.servicebus import ServiceBusClient, ServiceBusMessage
import azure.functions as func
connstr = "****"
topic_name = "***"
subscription_name = "***"
def get_engine(database='portal_rms', username='***', password='**', host='***', port=5432):
engine_string = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}"
engine = sqlalchemy.create_engine(engine_string)
return engine
def read_from_db(table_name, date_time):
acceleration_array = []
engine = get_engine()
connection = engine.connect()
metadata = sqlalchemy.MetaData()
pinconnector_attacheddevicelogdata = sqlalchemy.Table(
table_name, metadata, autoload=True, autoload_with=engine)
query = sqlalchemy.select([pinconnector_attacheddevicelogdata]).where(pinconnector_attacheddevicelogdata.columns.capture_time > date_time)\
.order_by(pinconnector_attacheddevicelogdata.columns.capture_time).limit(100)
ResultProxy = connection.execute(query)
ResultSet = ResultProxy.fetchall()
engine.dispose()
return ResultSet
def get_date_time(date_time_string):
[date, time] = date_time_string.split(" ")
[year, month, day] = date.split("-")
[hour, minute, second] = time.split(":")
[second, microsecond] = second.split(".")
[year, month, day, hour, minute, second, microsecond] = list(
map(lambda x: int(x), [year, month, day, hour, minute, second, microsecond]))
return datetime.datetime(year, month, day, hour, minute, second, microsecond, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None))
def write_date_time(date_time, address="date_time.txt"):
file1 = open(address, "w+")
file1.write((str(date_time)).split("+")[0])
file1.close()
def read_date_time(address="date_time.txt"):
file1 = open(address, "r")
date_time = file1.readline()
date_time = get_date_time(date_time)
file1.close()
return date_time
def get_query_latest_date_time(db_query, date_time):
for item in db_query:
if item["capture_time"] > date_time:
date_time = item["capture_time"]
return date_time
def transform(query_row):
query_row = dict(query_row)
for item in ['capture_time', 'received_time', 'read_time']:
query_row[item] = str(query_row[item]).split("+")[0]
return query_row
date_time = read_date_time()
db_query = read_from_db("pinconnector_attacheddevicelogdata", date_time)
write_date_time(db_query[-1]["capture_time"])
# print(db_query)
#data_send = json.dumps(db_query)
data_send = json.dumps(list(map(lambda x: transform(x), db_query)))
with ServiceBusClient.from_connection_string(connstr) as client:
with client.get_topic_sender(topic_name) as sender:
sender.send_messages(ServiceBusMessage(data_send))
【问题讨论】:
-
不要尝试在 Azure 函数中使用文件系统。不能保证在两次连续运行中使用同一台服务器来执行您的函数,因此即使您可以写入文件系统,您也可能会发现在后续运行中该文件似乎不存在。如果您需要在函数运行之间存储状态,请将其存储在其他位置,例如在 Azure 存储中。
标签: python postgresql azure-functions azureservicebus