python复制文件夹不阻塞_Python线程,线程不阻塞

我有一个Python程序,当我使用Ctrl-c退出应用程序时,脚本不会关闭。我的进程仍然显示在运行进程中。#!/usr/bin/env python

import socket

import threading

import Queue

import serial

import mysql.connector

from datetime import datetime, date, time

host = '0.0.0.0'

port = 1024

buffer = 102400

my_queue = Queue.Queue()

class readFromUDPSocket(threading.Thread):

def __init__(self, my_queue):

threading.Thread.__init__(self)

self.my_queue = my_queue

def run(self):

while True:

buffer1,addr = socketUDP.recvfrom(buffer)

self.my_queue.put(buffer1)

print 'UDP received'

class readFromSerial(threading.Thread):

def __init__(self, my_queue):

threading.Thread.__init__(self)

self.my_queue = my_queue

def run(self):

while True:

buffer2 = ser.readline(eol=';')

if buffer2:

self.my_queue.put(buffer2)

print 'Serial received'

class process(threading.Thread):

def __init__(self, my_queue):

threading.Thread.__init__(self)

self.my_queue = my_queue

self.alive = threading.Event()

self.alive.set()

def run(self):

while True:

buffer3 = self.my_queue.get()

today = datetime.now()

timestamp = today.strftime("%A, %B %d, %Y %H:%M:%S")

print 'Data pushed at:', timestamp

print buffer3

if buffer3.startswith('temp:'):

temp = buffer3.replace('temp:','')

cnx = mysql.connector.connect(user='root', password='xxxxx', database='temperature')

cursor = cnx.cursor()

cursor.execute("INSERT INTO temperature.temperature (time,temperature) VALUES (%s, %s)", [timestamp, temp])

print 'Data inserted into Database'

cnx.commit()

cursor.close()

cnx.close()

if __name__ == '__main__':

# Create socket (IPv4 protocol, datagram (UDP)) and bind to address

socketUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

socketUDP.bind((host, port))

ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=2)

# Instantiate & start threads

myServer = readFromUDPSocket(my_queue)

mySerial = readFromSerial(my_queue)

myDisplay = process(my_queue)

myServer.start()

myDisplay.start()

mySerial.start()

while 1:

pass

UDPSock.close()

ser.close()

为什么python线程不能用Ctrl+c关闭?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值