从Azure WebJob运行Python脚本

时间:2019-09-07 02:11:58

标签: c# python azure-webjobs processstartinfo

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

完成的工作
  1. 通过网址https://<webapp name>.scm.azurewebsites.net访问kudu工具,并通过“网站扩展”标签
  2. 安装Python 364x86
  3. 已确认Python 364x86已安装在以下路径中:D:\home\python364x86
  4. trading.py中添加了脚本D:\home\python364x86
  5. 使用此行代码run.bat创建的D:\home\python364x86\python.exe trading.py文件
  6. 在Webjob压缩文件中包含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\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

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脚本。 它在本地工作,但是一旦将其部署到生产环境中,它就会失败。尝试了很多次,花费了大量时间,但是仍然不确定为什么刺棒不起作用。感谢您的帮助。

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)

1 个答案:

答案 0 :(得分:3)

我试图通过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
    

    enter image description here

  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)
    

    enter image description here

    enter image description here

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

    enter image description here

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

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

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

    enter image description here

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

    enter image description here

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

    enter image description here

    enter image description here

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

enter image description here