从 Azure WebJob 运行 Python 脚本

问题描述 投票:0回答:1

我正在尝试从 Azure webjob 运行 python 脚本。这就是我按照这个link

所做的
  1. 通过 url 访问 kudu 工具
    https://<webapp name>.scm.azurewebsites.net
    并通过“站点扩展”选项卡安装
    Python 364x86
  2. 确认
    Python 364x86
    安装在以下路径:
    D:\home\python364x86
  3. trading.py
     中添加了我的脚本 
    D:\home\python364x86
  4. 使用这行代码创建了
    run.bat
    文件
    D:\home\python364x86\python.exe trading.py
  5. 网络作业 zip 文件中包含
    run.bat
    trading.py
  6. 已部署,但出现错误
[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)
c# python azure-webjobs processstartinfo
1个回答
9
投票

我尝试在 WebJob 中使用 Python 来实现您的需求并成功使其工作。

这是我的步骤和示例代码。我的本地环境是Windows 10上的Python 3.7。

  1. 创建Python虚拟环境并通过以下命令安装

    python-telegram-bot
    包。

     $ mkdir telegram-webjob
     $ virtualenv telegram-webjob
     $ cd telegram-webjob
     $ Scripts\active
     $ pip install python-telegram-bot
     $ pip freeze
    

  1. 我将您的代码更改为我的示例代码,然后在本地运行它可以正常工作,如下所示。

    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)
    

  1. 我创建了一个名为
    webjob
    的新目录,并将我的
    trading.py
    文件和所有目录及其文件复制到其中,如下所示。

  1. 编写启动bat脚本,名为

    run.bat
    ,代码如下。

     D:\home\python364x86\python.exe trading.py
    

D:\home\python364x86\python.exe
路径为
python364x86
站点扩展安装路径,如下图。

  1. 然后,我将
    webjob
    目录下的所有目录和文件打包为zip文件,如下。

  1. 我将其作为网络作业上传到Azure WebApp,如下所示,并启动它。

最后,它对我来说效果很好,我可以在我的电报客户端中看到消息间隔 10 秒。

© www.soinside.com 2019 - 2024. All rights reserved.