c# - 从 Azure WebJob 运行 Python 脚本

标签 c# python azure-webjobs processstartinfo

我正在尝试从 Azure webjob 运行 python 脚本。这就是我在 link 之后所做的

  1. 通过 url https://<webapp name>.scm.azurewebsites.net 访问 kudu 工具并安装了Python 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

函数.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。

  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

关于c# - 从 Azure WebJob 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830172/

相关文章:

python - pyGithub最大使用API​​调用率

c# - IQueryable Count 方法需要更长的时间来执行

c# - 使用三元运算符的方法调用

python - tkinter:停止文本小部件标签中的事件传播

python - 返回分布的 z 值 - python

azure - 如何在 AzureFunction V2 上的 ServiceBus BrokeredMessage 级别放弃消息或死信消息?

azure - PnP 继续 Webjob 不执行来配置网站集

c# - 我可以在没有 AzureWebJobsStorage 连接字符串的情况下执行 Azure webjobs 吗?

c# - 此类线程安全吗?

c# - 使用 C# 和 MVC3 的 HttpFileCollectionBase 问题上传多个文件