如何制作True:两个循环在python中同时运行

问题描述:

基本上我需要同时运行两个while循环。原因是我需要一个循环来更新GUI,另一个来检查程序是否连接到互联网。所以也许这需要多线程,或者我只是一个网页去解决我的问题。这里是我的源代码:如何制作True:两个循环在python中同时运行

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

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

def daMainProgram(): 

    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() 
+1

你'tkupdate'功能,在本质上,Tkinters'mainloop'是什么。您应该考虑使用主循环,以及如何使用'after'方法定期检查互联网连接。 – SneakyTurtle

+0

是的,我明白了,我通常使用主循环,但在这段代码中,我希望对tkupdate具有与internet_loop 相同的控制权,如果您可以给出答案,请回答而不是评论 –

+0

当您使用一个GUI库,真的应该只有**一个主循环。但是,许多库为您提供运行代码的方式,就像它是主循环的一部分一样。我在一段时间内没有完成过Tkinter,但是看到[你如何在Tkinter的事件循环旁边运行你自己的代码?](https://*.com/questions/459083/how-do-you-run-your- own-code-alongside-tkinters-event-loop)。这应该让你知道你需要做什么。 –

如何处理下面的内容。产生两个进程。每个人都可以并行运行,目标功能可以用自己的替换。

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 
... 
+0

出于某种原因,我尝试了这个使用你的代码,也在另一个项目上,它的工作,但是当我来到项目我问我得到这个错误: –

+0

该过程有分叉,并且不能安全地使用此CoreFoundation功能。你必须执行()。 歇上__THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY ___ YOU_MUST_EXEC __()调试(这是重复3次以上) Tcl_ServiceModeHook:通知未初始化 –

+0

@ Mr.Zeus你可能只需要产卵一个额外的进程(一个监控互联网连接),同时运行的主应用程序在现有的主流程中。为了从互联网检查过程中获取信息,您可能需要使用一些共享内存,您可以定期从主循环中检查更新。请参阅文档:https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Value –