从Azure WebJob运行Python脚本 [英] Running Python script from Azure WebJob
问题描述
我正在尝试从Azure webjob运行python脚本.这是我在
I'm trying to run python script from Azure webjob. This is what I've done following this link
- 通过URL
https://<webapp name>.scm.azurewebsites.net
访问kudu工具,并通过站点扩展"标签 安装 - 已确认
Python 364x86
已安装在以下路径中:D:\home\python364x86
- 在
D:\home\python364x86
中添加了我的脚本 - 使用此行代码
D:\home\python364x86\python.exe trading.py
创建的 - 包含在webjob zip文件中的
run.bat
和trading.py
- 已部署,但出现错误
Python 364x86
trading.py
run.bat
文件
- Access the kudu tool via the url
https://<webapp name>.scm.azurewebsites.net
and installedPython 364x86
via Site Extensions tab - Confirmed
Python 364x86
is installed in the following path:D:\home\python364x86
- Added my script
trading.py
inD:\home\python364x86
- Created
run.bat
file with this line of codeD:\home\python364x86\python.exe trading.py
- Included
run.bat
andtrading.py
in the webjob zip file - Deployed, but getting error
[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
Functions.cs
Functions.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脚本. 它在本地工作,但是一旦将其部署到生产环境中,它就会失败.尝试了很多次,花费了大量时间,但仍不确定为什么刺棒不起作用.感谢您的帮助.
Above code executes python script. It works locally, but once I deploy it to production it fails. Tried so many times, spent plethora of hours, but still unsure why prod doesn't work. Help is appreciated.
trading.py
trading.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来实现您的需求,并成功使之工作.
I tried to realize your needs with Python in WebJob and successfully make it works.
这是我的步骤和示例代码.我的本地环境是Windows 10上的Python 3.7.
Here is my steps and sample code. My local environment is Python 3.7 on Windows 10.
-
创建Python虚拟环境并通过以下命令安装
python-telegram-bot
软件包.
$ mkdir telegram-webjob
$ virtualenv telegram-webjob
$ cd telegram-webjob
$ Scripts\active
$ pip install python-telegram-bot
$ pip freeze
我将您的代码更改为示例代码,然后在本地运行,如下所示.
I changed your code as my sample code, then run it works fine on local, as below.
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)
我创建了一个名为webjob
的新目录,并将我的trading.py
文件和所有目录及其文件复制到其中,如下所示.
I created a new directoy named webjob
and copy my trading.py
file and all directories with their files into it, as below.
编写名为run.bat
的启动bat脚本,代码如下.
Write the startup bat script named run.bat
, the code as below.
D:\home\python364x86\python.exe trading.py
D:\home\python364x86\python.exe
路径是python364x86
网站扩展安装路径,如下图.
The D:\home\python364x86\python.exe
path is the python364x86
site extension installed path as the figure below.
然后,我将webjob
目录中的所有目录和文件打包为zip文件,如下所示.
Then, I packaged the all directories and files in the webjob
directory as a zip file, as below.
我将它作为webjob上传到Azure WebApp,如下图所示,并启动它.
I uploaded it to Azure WebApp as a webjob, as the figure below, and start it.
最后,它对我来说很好用,我可以看到电报客户端中显示的消息间隔为10秒.
Finally, it works fine for me and I can see the message interval 10 sec displayed in my telegram client.
这篇关于从Azure WebJob运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!