этот код не работает:(
#!/usr/bin/python
# -*- coding: utf-8 -*-
import thread, time
def myfunction(stop):
while True:
print '.'
if stop: break
time.sleep(0.3)
stop = False
thread.start_new_thread(myfunction,(stop,))
raw_input('')
stop = True
print 'exit'
точнее ему пофиг а моё нажатие Энтер и если после “print ‘exit’” ему вставить ещё один инпут() то он продолжит перечислять точки.
А этот код тоже не работает :(
#!/usr/bin/env python
# vim: sw=4 ts=4 expandtab ai
import time
import threading
import signal
class Trtest(threading.Thread):
def __init__(self,letter,flagstop):
self.letter = letter
self.flagstop = flagstop
super(Trtest, self).__init__()
def run(self):
while not self.flagstop.isSet():
print str(self), self.letter
time.sleep(1.0)
def join(self, timeout=1.0):
while self.isAlive():
super(Trtest, self).join(timeout)
def stop(self):
print "stopping", str(self)
self.flagstop.set()
fl1 = threading.Event()
fl2 = threading.Event()
t1 = Trtest('A',fl1)
t2 = Trtest('B',fl2)
signal.signal(signal.SIGUSR1, lambda *a: t1.stop())
signal.signal(signal.SIGUSR2, lambda *a: t2.stop())
t1.start()
t2.start()
t1.join()
t2.join()
Ждал ждал я его сигналов , так и не дождался
alex@alex-laptop ~ $ '/media/Files/Alex Doc/Programs/pro/pytHON/Учебные и мусорные коды/thread2.py'
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
^CTraceback (most recent call last):
File "/media/Files/Alex Doc/Programs/pro/pytHON/Учебные и мусорные коды/thread2.py", line 36, in <module>
t1.join()
File "/media/Files/Alex Doc/Programs/pro/pytHON/Учебные и мусорные коды/thread2.py", line 22, in join
super(Trtest, self).join(timeout)
File "/usr/lib/python2.6/threading.py", line 655, in join
self.__block.wait(delay)
File "/usr/lib/python2.6/threading.py", line 258, in wait
_sleep(delay)
KeyboardInterrupt
<Trtest(Thread-1, started -1217262736)> A
<Trtest(Thread-2, started -1225655440)> B
^CException KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
alex@alex-laptop ~ $ ^C