简体   繁体   English

在 Python 中运行两个脚本

[英]Running two scripts in Python

Dear people of the internet, I need some help... I have a Python script, main.py in which a user can enter 2 variables First, ReminderTime is an int, second, ReminderText is a string.亲爱的互联网人们,我需要一些帮助...我有一个 Python 脚本main.py ,用户可以在其中输入 2 个变量 首先, ReminderTime是一个整数,其次, ReminderText是一个字符串。 I want to send the two variables to another Python script, reminder.py .我想将这两个变量发送到另一个 Python 脚本reminder.py The reminder script will do stuff with the two objects, and then it will use ToastNotifier to make a Windows notification, before shutting down.提醒脚本将处理这两个对象,然后在关闭之前使用ToastNotifier发出 Windows 通知。 The main script will still run in the foreground during that time.在此期间,主脚本仍将在前台运行。 Is this possible?这可能吗?

You can import your other file reminder.py (make sure they are in the same path):您可以import您的其他文件reminder.py (确保它们在同一路径中):

import reminder

And then you can run a function from reminder.py inside main.py and pass your variables:然后你可以从main.py reminder.py main.py运行一个函数并传递你的变量:

reminder.your_function(ReminderTime, ReminderText)

You can use multiprocessing您可以使用多处理

Simple example:简单的例子:

p = Process(target=f, args=(ReminderTime, ReminderText))
p.start()

f - is function in your reminder.py that will start that script (like main in main.py by default) f - 是您的reminder.py main.py的函数,它将启动该脚本(默认情况下类似于main.pymain
args - is all arguments you need to send to this function args - 是你需要发送给这个函数的所有参数

Also, you need to import you function import reminder.function_name此外,您需要导入您的功能import reminder.function_name

This code will start you f function in background, so main code will still runing.此代码将在后台启动f函数,因此主代码仍将运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM