如何使 while(True):在 python 中同时运行两个循环 [英] How to make while(True): two loops run at same time in python

查看:210
本文介绍了如何使 while(True):在 python 中同时运行两个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要同时运行两个 while 循环.原因是我需要一个循环来更新 GUI,另一个循环来检查程序是否连接到互联网.所以也许这需要多线程,或者我可能只有一个网页就可以解决我的问题.这是我的源代码:

Basically I need to run two while loops at the same time. The reason being that I need one loop to update the GUI, and the other to check if the program is connected to the internet. So maybe this requires multi-threading, or maybe I am just one webpage away to fixing my problem. Here's my source code:

# -*- coding: utf-8 -*-

import sys
import urllib2
from Tkinter import *
import time
import socket

def TheMainProgram():
    airid = 'http://www.aviationweather.gov/metar/data?ids={airid}&format=raw&hours=0&taf=off&layout=off&date=0'.format(airid=e1.get())
    website = urllib2.urlopen(airid)
    website_html = website.read()
    data_start = website_html.find("<!-- Data starts here -->")
    br1 = data_start + 25
    br2 = website_html.find("<br />")
    metar_slice = website_html[br1:br2]
    print("Here is the undecoded METAR data:\n"+metar_slice)
    root2 = Tk()
    root2.title("#Conection_Made#")
    metar_dat_label = Label(root2, text="Here is the undecoded METAR data:")
    metar_dat_label_ln_2 = Label(root2, text=metar_slice)
    metar_dat_label.grid(row=0)
    metar_dat_label_ln_2.grid(row=1)    


def _quit():
    sys.exit()

def internet(host="8.8.8.8", port=53, timeout=3):
    try:
        socket.setdefaulttimeout(timeout)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
        return True
    except Exception:
        return False


def tkupdate():
    while True:
        root1.update()


def internet_loop():
    while True:
        out = internet()
        if out == True:
            connetion2internet.configure(text="YOU ARE CONNECTED TO THE INTERNET", fg="green")
        if out == False:
            connetion2internet.configure(text="YOU ARE NOT CONNECTED TO THE INTERNET", fg="red")


root1 = Tk()
root1.title("Welcome")
warning = Label(root1, text="*********** WARNING REQURIES INTERET TO RUN ***********")
warning.grid(row=0)
connetion2internet = Label(root1, text="")
connetion2internet.grid(row=1)
airport_code = Label(root1,text="Enter Airport Code:")
airport_code.grid(row=2, sticky="W")
e1 = Entry(root1)
e1.grid(row=2, sticky="E")
button1 = Button(root1, text="Contunue", command=daMainProgram).grid(row=3, sticky="W")
button2 = Button(root1, text="Quit", command=_quit)
button2.grid(row=3, sticky="E")
tkupdate()
internet_loop()

推荐答案

类似下面的内容怎么样.产生两个进程.每个都并行运行,目标函数可以替换为您自己的.

How about something like the following. Spawn two processes. Each one runs in parallel and the target functions can be replaced with your own.

from multiprocessing import Process
from time import sleep

def func1():
    while True:
        print("func1 up and running")
        sleep(1)

def func2():
    while True:
        print("func2 up and running")
        sleep(1)


if __name__ == '__main__':

    proc1 = Process(target=func1)
    proc1.start()

    proc2 = Process(target=func2)
    proc2.start()

输出为:

func1 up and running
func2 up and running
func1 up and running
func2 up and running
func1 up and running
func2 up and running
func1 up and running
func2 up and running
...

这篇关于如何使 while(True):在 python 中同时运行两个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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