Я очень хорошо отношусь к функциям Azure. Но мне удалось развернуть функцию и успешно запустить ее, когда выходные данные должны быть созданы в текущем каталоге. Никаких проблем, пока все в порядке.
Следующее, что мне нравится делать, это сохранять выходные данные в определенную папку. Это вызывает ошибку. Вскоре я перечислю ошибки.
Что мне хотелось бы знать, так это то, что как только я получу эту ошибку, даже если я вернусь к коду, который я проверил, он теперь выдает ту же ошибку. Да, я убедился, что сохранил файл, прежде чем запускать команду func start -verbose
Хорошо, вот рабочий код:
#Works NO issues
import azure.functions as func
import datetime
import logging
import subprocess
import os
app = func.FunctionApp()
@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False)
def ScrapyTimerTrigger(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')
week = 'week' # Set your desired week
year = '2023' # Set your desired year
game = 'hall-of-fame-weekend' # Set your desired game
local_file_name = f'{year}_{week}_{game}.json'
#local_file_path = os.path.join(os.getcwd(), local_file_name)
scrapy_command = [
'scrapy', 'crawl', 'NFLWeatherData',
'-a', f'Week={week}', '-a', f'Year={year}', '-a', f'Game={game}',
'-o', local_file_name # local_file_path
]
# Update this path to the correct directory
process = subprocess.Popen(scrapy_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=r"N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather")
stdout, stderr = process.communicate()
if process.returncode != 0:
logging.error(stderr)
else:
logging.info(stdout)
Это создаст файл без проблем.
Вот измененный код для записи в другое место.
import azure.functions as func
import datetime
import logging
import subprocess
import os
app = func.FunctionApp()
@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=True,
use_monitor=False)
def ScrapyTimerTrigger(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')
week = 'week' # Set your desired week
year = '2023' # Set your desired year
game = 'hall-of-fame-weekend' # Set your desired game
local_file_name = f'{year}_{week}_{game}.json'
local_file_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering", year, local_file_name)
# Create the directory if it does not exist
directory_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering", year)
os.makedirs(directory_path, exist_ok=True)
scrapy_command = [
'scrapy', 'crawl', 'NFLWeatherData',
'-a', f'Week={week}', '-a', f'Year={year}', '-a', f'Game={game}',
'-o', local_file_path
]
# Update this path to the correct directory
process = subprocess.Popen(scrapy_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=r"N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather")
stdout, stderr = process.communicate()
if process.returncode != 0:
logging.error(stderr)
else:
logging.info(stdout)
Вот код ошибки, который выдает этот скрипт
[2024-06-24T00:41:45.459Z] Received WorkerInitRequest, python version 3.11.9 | packaged by Anaconda, Inc. | (main, Apr 19 2024, 16:40:41) [MSC v.1916 64 bit (AMD64)], worker version 4.28.1, request ID 583ffc8a-a82c-49d4-b586-a0ded194964c. App Settings state: PYTHON_THREADPOOL_THREAD_COUNT: 1000 | PYTHON_ENABLE_WORKER_EXTENSIONS: False. To enable debug level logging, please refer to https://aka.ms/python-enable-debug-logging
[2024-06-24T00:41:45.767Z] Received WorkerMetadataRequest, request ID 583ffc8a-a82c-49d4-b586-a0ded194964c, function_path: N:\github\SportsData\NFLWeather\FunctionLocal\NFLWeather\NFLWeatherFunctionApp\function_app.py
[2024-06-24T00:41:45.797Z] Worker failed to index functions
[2024-06-24T00:41:45.799Z] Result: Failure
Exception: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1269-1270: malformed \N character escape (function_app.py, line 43)
Stack: File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 413, in _handle__functions_metadata_request
self.load_function_metadata(
File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 393, in load_function_metadata
self.index_functions(function_path, function_app_directory)) \
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 765, in index_functions
indexed_functions = loader.index_function_app(function_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "M:\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\loader.py", line 238, in index_function_app
imported_module = importlib.import_module(module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "M:\Miniconda3\envs\Azure\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1204, in _gcd_import
File "", line 1176, in _find_and_load
File "", line 1147, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 936, in exec_module
File "", line 1074, in get_code
File "", line 1004, in source_to_code
File "", line 241, in _call_with_frames_removed
Вот чего я не понимаю, и мне хотелось бы знать, почему и что мне нужно сделать, чтобы рабочий файл снова заработал. Есть ли какой-то кеш или что-то, что нужно очистить, чтобы рабочий файл снова заработал. Это позволит мне внести изменения в расположение пути и добавить что-нибудь работающее.
Также это некоторые из вариантов, которые я пробовал
# local_file_path = os.path.join("N:\\github\\SportsData\\NFLWeather\\Data_Gathering\\2024", local_file_name)
# local_file_path = os.path.join(r"N:\\github\\SportsData\\NFLWeather\\Data_Gathering\\2024", local_file_name)
# local_file_path = Path("N:/github/SportsData/NFLWeather/Data_Gathering/2024") / local_file_name
# local_file_path = Path(r"N:\github\SportsData\NFLWeather\Data_Gathering\2024") / local_file_name
Подробнее здесь: https://stackoverflow.com/questions/786 ... deployment