我正在尝试从 Azure webjob 运行 python 脚本。这就是我在 link 之后所做的
- 通过 url
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)
我创建了一个名为
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 秒。
关于c# - 从 Azure WebJob 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830172/