使用多个 Python 应用程序构建存储库 [英] Structure a repository with several Python applications

查看:37
本文介绍了使用多个 Python 应用程序构建存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入共享相同代码资源的应用程序的最佳做法是什么?

What are the best practices for importing applications that share the same code resources?

假设我有一个自动股票交易器,它包含两个独立运行的服务(不同的机器).这两项服务是:

Imagine I have an automated stock trader that contains two services that run independently (different machines). The two services are:

  • collection_service - 每分钟收集一次股票价格并将其存储到 SQL 数据库中
  • decision_making - 每十分钟(根据收集的数据)做出是否购买股票的决定.
  • collection_service - collects stock prices every minute and stores it to a SQL database
  • decision_making - makes a decision every ten minutes (based on the collected data) whether or to buy a stock.

为了维护 SSOT,他们都使用相同的 SQL 表模型(比如 SQLalchemy 模型),但是它们每个都有不同的依赖关系.此外,他们都使用我公司在不同项目中编写的代码.

With the desire to maintain SSOT they both use the same SQL table models (say SQLalchemy models), however they each have different dependencies. I addition they all use code thats's written by my company in different projects.

我的存储库如下所示:

─my_companies_repo
    ├───auto_trader
    │   ├───collection
    │   │       main_collection.py
    │   │       requirements.txt
    │   │
    │   ├───db_manage
    │   │       sql_models.py
    │   │
    │   └───decision_making
    │           main_decision.py
    │           requirements.txt
    │
    └───common

import 语句会是什么样子,我应该在运行应用程序时传递多个 PYTHONPATH 还是只有一个根?

How would import statements will look like, should I pass several PYTHONPATHs when running the application or have one root?

例如:

main_decision.py

from auto_trader.db_manage.sql_models
# or pass two PYTHONPATH's (one for common and one for auto_trader) and do this:
from db_manage.sql_models

推荐答案

因为你的代码布局是 3 个包......但可能只有 1 个代码包......你可以考虑做这样的事情

As your code -layout is 3 packages ... But possibly in just 1 code package.... you can consider doing something like this

它看起来像这样

#!/usr/bin/env python

from setuptools import setup

setup(
    name='stocks',
    version='0.3',
    description='foo',
    author='bar',
    packages = ['my_companies_repo.auto_trader.collection',
                'my_companies_repo.auto_trader.db_manage',
              'my_companies_repo.auto_trader.decision_making'])

如果要构建 Python 分发包的第一步

First step if to build a Python Distribution package

python setup.py sdist

刚刚创建了一个类似这样的文件

That just created a file something like this

project/sdist/stocks-03.tar.gz 

您现在将该文件移动到托管此部分的 3 个服务器

You now move that file to your 3 servers where this parts are being hosted

  • 收藏
  • db_manage
  • 决策

所以

scp /sdist/stocks-03.tar.gz server:~/

现在您登录这些机器 - 然后安装

Now you log into those machines - and then install

pip3 install stocks-03.tar.gz 

您必须在每台机器上执行此操作(这会变得乏味 - 有一个要解决的问题...但我正在努力使事情变得简单).

You have to do this on each machine (this gets tedious - there is a was to solve... but I am trying to keep things simple).

此时所有机器上都安装了相同的软件......

At this point the same software is now on all the machines ...

那么我们如何运行它......

So how do we run it ....

在收藏机上

python3 -m 'my_companies_repo.auto_trader.collection'  

在决策机上

python3 -m 'my_companies_repo.auto_trader.decision_making'

这篇关于使用多个 Python 应用程序构建存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆