【问题标题】:can I run multiple def in one time我可以一次运行多个def
【发布时间】:2022-07-27 00:00:46
【问题描述】:

我想像这样一次运行多个def

def one() :
    print("hi")

def two() :
    print("hi2")

def three() :
    print("hi3")

main.py:

from one import one

from two import two

from three import three

one()
two()
three()

我想运行这些命令一次:

one()
two()
three()

我可以这样做吗?

【问题讨论】:

  • 你试过运行它们吗?
  • 这些函数是否都在同一个文件中?您的代码 sn-p 看起来像,但导入提示并非如此
  • 不,每一个都在一个单独的文件中
  • 不,我没有尝试过,但这只是一个示例,我实际上将放置一个长脚本而不是每个脚本。

标签: python python-3.x selenium-webdriver web webdriver


【解决方案1】:

使用线程

import threading

def function_1():
   print("1")

def function_2():
   print("2")

Thread1 = threading.Thread(target=function_1)
Thread2 = threading.Thread(target=function_2)

Thread1.start()
Thread1.start()
Thread1.join()
Thread2.join()

或者你可以使用多处理

from multiprocessing import Process

proccess1 = Process(target=function_1)
proccess2 = Process(target=function_2)

proccess1.start()
proccess2.start()

proccess1.join()
proccess2.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2015-09-20
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多