我有一个代码,每 100ms 打印出鼠标坐标(x,y)和时间戳。我想要跑10秒。就是这样。
所以我实施了" 多处理"我将其初始化为从多处理函数中调用主函数" printevery100ms" ,并告诉它在10秒后关闭。
但不是在10秒后关闭,而是跳过p.terminate()命令并继续运行并运行......
以下是代码。
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秒后关闭它,这是terminate命令的目的。我写了一个打印命令打印出来"没有终止"如果代码在10秒后仍然没有停止;这正是发生的事情。
结果如下: (9秒后......)
("{'x': 1255, 'y': 395}", '2017-01-19 13:31:58')
("{'x': 1255, 'y': 395}", '2017-01-19 13:31:58')
("{'x': 1255, 'y': 395}", '2017-01-19 13:31:58')
Not Terminated
("{'x': 1205, 'y': 399}", '2017-01-19 13:31:58')
("{'x': 961, 'y': 410}", '2017-01-19 13:31:58')
("{'x': 610, 'y': 407}", '2017-01-19 13:31:58')
你可以看到它的打印"没有终止"这意味着它运行10秒并且它到达terminate命令,但继续运行,好像什么也没发生。你能告诉我我做错了什么,或者这与多处理有关吗?
答案 0 :(得分:0)
您的代码存在一些问题:
if __name__ == '__main__':
函数中有不同的缩进。multiprocessing.Process
在你的printevery100ms
之前调用(右边第30行)并在主线程中运行该功能并且不会停止。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")
的调用使用参数(10,)调用customMaven(MavenPublication) {
artifactId 'myArtifactId'
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.specialConfiguration.getResolvedConfiguration().getFirstLevelModuleDependencies().each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.moduleGroup)
dependency.appendNode('artifactId', it.moduleName)
dependency.appendNode('version', it.moduleVersion)
}
}
}
,导致此调用失败。但是,您仍然运行主线程,因此它似乎不会停止。以下是修复:
{{1}}