python多线程
来源:互联网 发布:2017年九宫图算法 编辑:程序博客网 时间:2024/04/28 18:01
#两个线程一个发心跳,一个往打印机发数据#
#!/usr/local/bin/python2.7
# encoding: utf-8
import threading
import socket
import time
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
import ConfigParser
'''
Created on 2016年07月7日
@author: wuxiaobing
Mail:2683904575@qq.com
history:
V1.0增加多线程机制,一个线程是和打印机做心跳连接、另一个线程发数据到打印机。(2016-07-15)
V1.1将所有配置文件用模块ConfigParser整理到一个配置文件,按配置文件读写方式处理。(2016-07-25)
'''
conf = ConfigParser.ConfigParser()
conf.read("D:\\test.conf")
HOST = conf.get("section1", "ip")
PORT = int(conf.get("section1", "port"))
time1 = conf.get("section2", "t1")
time2 = conf.get("section2", "t2")
def lan_send_data_printer():
BUFFERSIZE = 1024
ADDR = (HOST, PORT)
count=0
while(1):
count=count+1
TCPClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
TCPClient.connect(ADDR)
except socket.error, e:
print "Connection error: %s" % e
continue
looger.info(u"V1.1&正在运行第 %s 次" % str(count))
TCPClient.close()
print HOST+"--->"+"threading1"+"--->"+u"发心跳"+" "+str(count)+u"次"
time.sleep(int(time1))
def loop2(title,txt):
BUFFERSIZE = 1024
ADDR = (HOST, PORT)
countt=0
while (1):
countt=countt+1
TCPClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
TCPClient.connect(ADDR)
except socket.error, e:
print "Connection error: %s" % e
print u"亲,打印机IP是不被人占了或没开打印机?"
continue
looger.info(u"V1.1&正在运行第 %s 次" % str(countt))
TCPClient.send(txt)
TCPClient.close()
print HOST+"--->"+"threading2"+"--->"+u"打印数据"+" "+str(countt)+u"次"
time.sleep(int(time2))
if __name__ == '__main__':
looger = logging.getLogger('threading2')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
LogHandLer=TimedRotatingFileHandler(r'E:\countLAN.log',when="midnight")
LogHandLer.suffix="%Y%m%d-%H%M.log"
LogHandLer.setFormatter(formatter)
looger.addHandler(LogHandLer)
looger.setLevel(logging.INFO)
f2 = open(r"D:\Users\Out123.spc",'rb')
lines2 = f2.read()
f2.close()
t = threading.Thread(target=lan_send_data_printer)
t2=threading.Thread(target=loop2,args=("lines2",lines2))
t.start()
t2.start()
- Python多线程
- Python多线程
- Python多线程
- python多线程
- python 多线程
- python多线程
- python多线程
- python 多线程
- python多线程
- Python多线程
- Python 多线程
- python多线程
- python 多线程
- {python多线程}
- Python 多线程
- Python多线程
- python 多线程
- Python 多线程
- Xcode7 创建自定义xctemplate Xcode生成默认注释
- Unity 5.0之后 Animation.AddClip 无效
- VS2012配置Opengl以及“无法解析的外部符号 __imp____glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用”问题
- webservice-客户端调用cxf https服务端
- DIV 设置 display:inline-block 导致 DIV 对不齐的解决方法
- python多线程
- Linux硬盘分区满,但没有找到占用文件
- (二)使用JODConverter将office文档转换为pdf
- Linux监控面板linux-dash的安装过程
- 强连通分量——Kosaraju算法
- java中volatile关键字的解释
- VR晕眩原因及解决方法
- pyqt5+eric6 openfile
- 8086汇编语言 retf指令实例