为什么我的Python代码在10秒后仍然运行?
我有一个代码,每100毫秒打印一次鼠标坐标(x,y)和时间戳。我想让它运行10秒钟。就这样 因此,我实现了“多处理”,并对其进行初始化,从多处理函数内部调用主函数“printevery100ms”,并告诉它在10秒后关闭 但它没有在10秒后关闭,而是跳过p.terminate()命令,继续运行 这是代码。为什么我的Python代码在10秒后仍然运行?,python,python-2.7,multiprocessing,python-multithreading,terminate,Python,Python 2.7,Multiprocessing,Python Multithreading,Terminate,我有一个代码,每100毫秒打印一次鼠标坐标(x,y)和时间戳。我想让它运行10秒钟。就这样 因此,我实现了“多处理”,并对其进行初始化,从多处理函数内部调用主函数“printevery100ms”,并告诉它在10秒后关闭 但它没有在10秒后关闭,而是跳过p.terminate()命令,继续运行 这是代码。 import multiprocessing import time import threading import datetime def printevery100ms():
import multiprocessing
import time
import threading
import datetime
def printevery100ms():
threading.Timer(.1,printevery100ms).start()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
from ctypes import windll,Structure,c_int,byref
class POINT(Structure):
_fields_ = [("x",c_int),("y",c_int)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return {"x": pt.x,"y": pt.y}
pos = queryMousePosition()
print('{{\'x\': {}, \'y\': {}}}'.format(pos['x'],pos['y']),st)
printevery100ms()
if __name__ == '__main__':
# Start printevery100ms as a process
p = multiprocessing.Process(target=printevery100ms, name="printevery100ms", args=(10,))
p.start()
# Wait 10 seconds for printevery100ms
time.sleep(10)
# Terminate printevery100ms
p.terminate()
p.join()
print "Not Terminated"
因此,多处理代码初始化10秒计时器,并在10秒后关闭它,这是终止命令的目的。我写了一个print命令,如果代码在10秒后仍然没有停止,就打印出“notterminated”;事实就是这样
结果如下:(9秒后..)
正如您所看到的,它正在打印“notterminated”,这意味着它运行10秒,然后到达terminate命令,但继续运行,就好像什么也没发生一样。您能告诉我我做错了什么,或者这是否与多重处理有关吗?您的代码有一些问题:
printeevery100ms
函数中有不同的缩进printedevery100ms()
在调用之前,如果uuu name\uuuuu=='\uuuuu main\uuuu':
(关于第30行)并在主线程中运行函数,并且不会停止multiprocessing.Process
调用printerevery100ms
,参数为(10,),导致调用失败。但是,您仍然可以运行主线程,因此它看起来不会停止import multiprocessing
import time
import threading
import datetime
def printevery100ms():
threading.Timer(.1,printevery100ms).start()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
from ctypes import windll,Structure,c_int,byref
class POINT(Structure):
_fields_ = [("x",c_int),("y",c_int)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return {"x": pt.x,"y": pt.y}
pos = queryMousePosition()
print('{{\'x\': {}, \'y\': {}}}'.format(pos['x'],pos['y']),st)
#printevery100ms() # HERES ISSUE NUMBER 2
if __name__ == '__main__':
# Start printevery100ms as a process
p = multiprocessing.Process(target=printevery100ms, name="printevery100ms", args=()) # FIXED ISSUE NUMBER 2
p.start()
# Wait 10 seconds for printevery100ms
time.sleep(10)
# Terminate printevery100ms
p.terminate()
p.join()
print("Not Terminated")
您的代码存在一些问题:
printeevery100ms
函数中有不同的缩进printedevery100ms()
在调用之前,如果uuu name\uuuuu=='\uuuuu main\uuuu':
(关于第30行)并在主线程中运行函数,并且不会停止multiprocessing.Process
调用printerevery100ms
,参数为(10,),导致调用失败。但是,您仍然可以运行主线程,因此它看起来不会停止import multiprocessing
import time
import threading
import datetime
def printevery100ms():
threading.Timer(.1,printevery100ms).start()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
from ctypes import windll,Structure,c_int,byref
class POINT(Structure):
_fields_ = [("x",c_int),("y",c_int)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return {"x": pt.x,"y": pt.y}
pos = queryMousePosition()
print('{{\'x\': {}, \'y\': {}}}'.format(pos['x'],pos['y']),st)
#printevery100ms() # HERES ISSUE NUMBER 2
if __name__ == '__main__':
# Start printevery100ms as a process
p = multiprocessing.Process(target=printevery100ms, name="printevery100ms", args=()) # FIXED ISSUE NUMBER 2
p.start()
# Wait 10 seconds for printevery100ms
time.sleep(10)
# Terminate printevery100ms
p.terminate()
p.join()
print("Not Terminated")