如何合并两个python文件作为一个文件运行?

时间:2018-11-25 06:09:03

标签: python function main

我有两个运行良好的不同python文件。我只想将它们作为一个运行。

app.py

#!/usr/bin/env python
from importlib import import_module
import os
from flask import Flask, render_template, 
Response

# import camera driver
if os.environ.get('CAMERA'):
    Camera = import_module('camera_' + 
os.environ['CAMERA']).Camera
else:
#from camera import Camera
# Raspberry Pi camera module (requires 
picamera package)
    from camera_pi import Camera

app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen(camera):
    """Video streaming generator 
function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: 
image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in 
the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x- 
mixed-replace; boundary=frame')


if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True)

此代码来自互联网,我想在这里添加我的代码,以便在我运行此代码时也可以运行我的代码。这是我的代码。

beam.py

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import MySQLdb

db = MySQLdb.connect("localhost", "root", 
"raspberry", "cribdb")

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12, GPIO.IN) #Right level-1


last_status = None

while True:
    input12 = GPIO.input(12)


    if (input12 == 1):
        status = "Lying down"
    else:
        status = "Out of the crib"

    time.sleep(1)

    if status != last_status:
        print(status)
        last_status = status
        curs = db.cursor()
        curs.execute(
        """INSERT INTO tbstatus values(NULL, %s)""", (status,)
)
    db.commit()

    number_of_rows= curs.execute("SELECT * FROM tbstatus")

    if (number_of_rows >= 2):
        curs.execute("""DELETE FROM tbstatus order by id LIMIT 1""")
        db.commit()

是否可以同时运行它们?我试图在两个不同的终端中运行它们,并且它们都在运行。我只希望它们作为一个python文件运行。

0 个答案:

没有答案