
背景:Python脚本:读取文件中每行,放入列表中;循环读取列表中的每个元素,并做处理 *** 作。
模块:threading
第一部分:
:多线程脚本 (该脚本只有两个线程,t1循环次数<t2)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
#!/usr/bin/env python#-- coding: utf8 -- import sysimport timeimport stringimport threadingimport datetimefileinfo = sysargv[1] # 读取文件内容放入列表host_list = []port_list = [] # 定义函数:读取文件内容放入列表中def CreateList(): f = file(fileinfo,'r') for line in freadlines(): host_listappend(linesplit(' ')[0]) port_listappend(linesplit(' ')[1]) return host_list return port_list fclose() # 单线程 循环函数,注释掉了#def CreateInfo(): # for i in range(0,len(host_list)): # 单线程:直接循环列表# timesleep(1)# TimeMark = datetimedatetimenow()strftime('%Y-%m-%d %H:%M:%S')# print "The Server's HostName is %-15s and Port is %-4d !!! [%s]" % (host_list[i],int(port_list[i]),TimeMark)# # 定义多线程循环调用函数def MainRange(start,stop): #提供列表index起始位置参数 for i in range(start,stop): timesleep(1) TimeMark = datetimedatetimenow()strftime('%Y-%m-%d %H:%M:%S') print "The Server's HostName is %-15s and Port is %-4d !!! [%s]" % (host_list[i],int(port_list[i]),TimeMark) # 执行函数,生成列表CreateList()# 列表分割成:两部分 mid为列表的index中间位置mid = int(len(host_list)/2) # 多线程部分threads = []t1 = threadingThread(target=MainRange,args=(0,mid))threadsappend(t1)t2 = threadingThread(target=MainRange,args=(mid,len(host_list)))threadsappend(t2) for t in threads: tsetDaemon(True) tstart()tjoin()print "ok"
以上是脚本内容!!!
----------------------------------------------------------------------
:读取文件的内容
文件内容:
[root@monitor2 logdb]# cat hostinfotxt
1921681011 1011
1921681012 1012
1921681013 1013
1921681014 1014
1921681015 1015
1921681016 1016
1921681017 1017
1921681018 1018
1921681019 1019
1921681020 1020
1921681021 1021
1921681022 1022
1921681023 1023
1921681024 1024
1921681025 1025
:输出结果:
单线程 : 执行脚本:输出结果:
[root@monitor2 logdb]# /Threadforpy hostinfotxt
The Server's HostName is 1921681010 and Port is 1010 !!! [2017-01-10 14:25:14]
The Server's HostName is 1921681011 and Port is 1011 !!! [2017-01-10 14:25:15]
The Server's HostName is 1921681012 and Port is 1012 !!! [2017-01-10 14:25:16]
The Server's HostName is 1921681025 and Port is 1025 !!! [2017-01-10 14:25:29]
多线程:执行脚本:输出 结果
[root@monitor2 logdb]# /Threadforpy hostinfotxt
The Server's HostName is 1921681011 and Port is 1011 !!! [2017-01-10 14:51:51]
The Server's HostName is 1921681018 and Port is 1018 !!! [2017-01-10 14:51:51]
The Server's HostName is 1921681012 and Port is 1012 !!! [2017-01-10 14:51:52]
The Server's HostName is 1921681019 and Port is 1019 !!! [2017-01-10 14:51:52]
The Server's HostName is 1921681013 and Port is 1013 !!! [2017-01-10 14:51:53]
The Server's HostName is 1921681020 and Port is 1020 !!! [2017-01-10 14:51:53]
The Server's HostName is 1921681014 and Port is 1014 !!! [2017-01-10 14:51:54]
The Server's HostName is 1921681021 and Port is 1021 !!! [2017-01-10 14:51:54]
The Server's HostName is 1921681015 and Port is 1015 !!! [2017-01-10 14:51:55]
The Server's HostName is 1921681022 and Port is 1022 !!! [2017-01-10 14:51:55]
The Server's HostName is 1921681016 and Port is 1016 !!! [2017-01-10 14:51:56]
The Server's HostName is 1921681023 and Port is 1023 !!! [2017-01-10 14:51:56]
The Server's HostName is 1921681017 and Port is 1017 !!! [2017-01-10 14:51:57]
The Server's HostName is 1921681024 and Port is 1024 !!! [2017-01-10 14:51:57]
The Server's HostName is 1921681025 and Port is 1025 !!! [2017-01-10 14:51:58]
使用python自带的gui就可以实现,python自带tk,使用tk的mainloop就会让python程序一直运行,只要让gui窗口一直不显示就是一直在后动员运行了。代码示意如下:
from
tkinter
import
root
=
tk()
rootwithdraw()
#
隐藏窗口
rootmainloop()
#
消息循环
把代码以pyw扩展名保存,执行一下,就会让程序一直在后台执行,可以通过任务管理器结束,如下:
是倒的。
题目的主要目标是实现阶加运算。图中阶加功能是使用递归函数f()来实现的。
要想使这一功能循环,自然有规则要循环体中完成输入、调用f()、输出。
因此,可以修改为:
#def f(x):
# if x > 0:
# return x + f(x-1)
# else
# return 0
f = lambda x: 0 if x == 0 else f(x - 1) + x
a = input('input:')
while aisdigit():
print(f(int(a)))
a=input('input:') #再次
else:
print('input is not digit')
以上就是关于python循环怎么用多线程去运行全部的内容,包括:python循环怎么用多线程去运行、python如何程序一直运行、(python)请问怎么让这段程序循环进行。(这只能用一次)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)