[英]Running Python script from Azure WebJob
我正在嘗試從 Azure webjob 運行 python 腳本。 這是我按照此鏈接所做的
https://<webapp name>.scm.azurewebsites.net
訪問 kudu 工具並通過站點擴展選項卡安裝Python 364x86
Python 364x86
安裝在以下路徑: D:\\home\\python364x86
trading.py
在D:\\home\\python364x86
run.bat
文件D:\\home\\python364x86\\python.exe trading.py
run.bat
和trading.py
在webjob zip文件[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Initializing
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Run script 'run.bat' with script host - 'WindowsScriptHost'
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Running
[09/07/2019 07:02:00 > 0dd02c: ERR ] The filename, directory name, or volume label syntax is incorrect.
[09/07/2019 07:02:00 > 0dd02c: INFO]
[09/07/2019 07:02:00 > 0dd02c: INFO] D:\local\Temp\jobs\triggered\z\2az54ret.wh4>D:\home\python364x86\python.exe trading.py
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Failed
[09/07/2019 07:02:00 > 0dd02c: SYS ERR ] Job failed due to exit code 1
函數.cs
public void StartTheBot()
{
// Local
//var fileName = @"C:\Users\robert\AppData\Local\Programs\Python\Python37-32\python.exe";
//var script = @"C:\python-scripts\trading.py";
// Production
var fileName = @"D:\home\python364x86\python.exe";
var script = @"D:\home\python364x86\trading.py";
var errors = "";
var results = "";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = script,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
}
Console.WriteLine("Errors:");
Console.WriteLine(errors);
Console.WriteLine();
Console.WriteLine("Results:");
Console.WriteLine(results);
}
上面的代碼執行python腳本。 它可以在本地工作,但是一旦我將它部署到生產環境,它就會失敗。 嘗試了很多次,花了很多時間,但仍然不確定為什么 prod 不起作用。 幫助表示贊賞。
交易.py
import telegram
my_token = 'mytoken'
bot = telegram.Bot(token = my_token)
chat_id = 'mychatid'
message = 'Hello
bot.sendMessage(chat_id=chat_id, text=message)
我嘗試在 WebJob 中使用 Python 實現您的需求並成功使其工作。
這是我的步驟和示例代碼。 我的本地環境是 Windows 10 上的 Python 3.7。
創建一個 Python 虛擬環境並通過如下命令安裝python-telegram-bot
包。
$ mkdir telegram-webjob $ virtualenv telegram-webjob $ cd telegram-webjob $ Scripts\\active $ pip install python-telegram-bot $ pip freeze
我將您的代碼更改為我的示例代碼,然后在本地運行它可以正常工作,如下所示。
import telegram from datetime import datetime as dt my_token = '<my token>' bot = telegram.Bot(token = my_token) chat_id = '<chat id>' message = f"Hello at {dt.now()}" bot.sendMessage(chat_id=chat_id, text=message)
我創建了一個新的名為directoy webjob
和復制我trading.py
文件,並與他們的文件到它的所有目錄,如下圖所示。
編寫名為run.bat
的啟動 bat 腳本,代碼如下。
D:\\home\\python364x86\\python.exe trading.py
D:\\home\\python364x86\\python.exe
路徑為python364x86
站點擴展安裝路徑,如下圖。
然后,我將webjob
目錄下的所有目錄和文件打包成zip文件,如下。
我把它作為webjob上傳到Azure WebApp,如下圖,並啟動它。
最后,它對我來說很好用,我可以看到電報客戶端中顯示的消息間隔為 10 秒。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.