运行 Python 来自 Azure WebJob 的脚本

Running Python script from Azure WebJob

我正在尝试从 Azure webjob 运行 python 编写脚本。这就是我在

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

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 不起作用。感谢帮助。

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 实现您的需求并成功实现它。

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

  1. 创建一个 Python 虚拟环境并通过以下命令安装 python-telegram-bot 包。

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

  2. 我把你的代码改成了我的示例代码,然后运行它在本地运行正常,如下。

    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)
    

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

  4. 编写名为run.bat的启动bat脚本,代码如下

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

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

  5. 然后,我将webjob目录下的所有目录和文件打包成一个zip文件,如下。

  6. 我把它作为webjob上传到Azure WebApp,如下图,然后启动。

最后,它对我来说工作正常,我可以在我的电报客户端中看到消息间隔 10 秒。