Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

python - How to run the functions periodically while main is waiting for the server close

I have experience in C programming and new to Python. I would like to run fucntion1 and fucntion2 continuously instead of waiting for server to be closed. Right now the below code run the functions once and wait for the server. How to run the functions? Please help

async def main():
    function1()
    function2()
    server = await websockets.serve(
        on_connect,
        '192.168.1.251',
        9000,
        subprotocols=['ocpp1.6'],
        ping_interval = 10,
        ping_timeout = 120
        )
    logging.info("Server Started listening to new connections...")
    await server.wait_closed()
question from:https://stackoverflow.com/questions/65840182/how-to-run-the-functions-periodically-while-main-is-waiting-for-the-server-close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can run the function 1 and 2 in two separate threads.

from threading import Thread
t1 = Thread(target = function1)
t2 = Thread(target = function2)
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
<your code to connect to server>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...