【问题标题】:Result: Failure Exception: OSError: [Errno 30] Read-only file system on Python and Azure Function结果:失败异常:OSError:[Errno 30] Python 和 Azure Function 上的只读文件系统
【发布时间】: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


【解决方案1】:

原因可能是操作系统版本不兼容。在这种情况下请考虑custom container

您也可以参考这个类似的问题:https://docs.microsoft.com/en-us/answers/questions/67126/failure-exception-oserror-errno-30-read-only-file.html

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2020-05-19
    • 2021-06-19
    • 2022-11-23
    • 2018-10-05
    • 2021-11-19
    • 2022-06-23
    • 2020-11-15
    • 2020-07-01
    相关资源
    最近更新 更多