从 Azure WebJob 运行 Python 脚本

bbu*_*ver 6 c# python processstartinfo azure-webjobs

我正在尝试从 Azure webjob 运行 python 脚本。这是我按照此链接所做的

  1. 通过 url 访问 kudu 工具https://<webapp name>.scm.azurewebsites.netPython 364x86通过站点扩展选项卡安装
  2. 确认Python 364x86安装在以下路径:D:\home\python364x86
  3. 加入我的脚本trading.pyD:\home\python364x86
  4. run.bat用这行代码创建文件D:\home\python364x86\python.exe trading.py
  5. 包括run.battrading.py在webjob zip文件
  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
Run Code Online (Sandbox Code Playgroud)

函数.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);
        }
Run Code Online (Sandbox Code Playgroud)

上面的代码执行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)
Run Code Online (Sandbox Code Playgroud)

Pet*_*Pan 6

我试图在 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
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明

  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)
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明

    在此处输入图片说明

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

    在此处输入图片说明

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

    D:\home\python364x86\python.exe trading.py
    
    Run Code Online (Sandbox Code Playgroud)

    D:\home\python364x86\python.exe路径是python364x86如下图所示的网站扩展安装路径。

    在此处输入图片说明

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

    在此处输入图片说明

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

    在此处输入图片说明

    在此处输入图片说明

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

在此处输入图片说明