小编Man*_*anu的帖子

Python 守护线程在 Windows 上不存在

我正在尝试理解Python 中的守护线程。我的理解是,一旦主线程退出或非守护线程被杀死,守护线程就会自动被杀死。然而,在 Windows 机器上,这不是我的观察结果。

import threading
import time


def print_work_a():
    print('Starting of thread :', threading.currentThread().name)
    time.sleep(2)
    print('Finishing of thread :', threading.currentThread().name)


def print_work_b():
    print('Starting of thread :', threading.currentThread().name)
    print('Finishing of thread :', threading.currentThread().name)

a = threading.Thread(target=print_work_a, name='Thread-a', daemon=True)
b = threading.Thread(target=print_work_b, name='Thread-b')

a.start()
b.start()
Run Code Online (Sandbox Code Playgroud)

观察到的输出:

>>> Starting of thread :Thread-a
Starting of thread :Thread-b


Finishing of thread :Thread-b

Finishing of thread :Thread-a
Run Code Online (Sandbox Code Playgroud)

我预计输出不会包含Finishing of thread :Thread-a因为到那时非守护线程将被杀死,因此守护线程也将被杀死。导致守护线程保持活动状态的代码中的错误是什么?

python python-multithreading python-3.x

3
推荐指数
1
解决办法
1596
查看次数

和python中的操作

以下是产生令人困惑的结果的代码。

a = None
b = 1
print a and b
if a and b is None:
    print "True"
else:
    print "False"
Run Code Online (Sandbox Code Playgroud)

这里 bool(a) 是假的,因为它没有。所以通过短路,表达式应该返回 None。这实际上正在发生。但是,在 if 条件期间,条件失败。尽管 a 和 b 产生 None,但条件失败并在输出中显示 false。有人可以解释为什么会这样吗?

python operator-precedence

1
推荐指数
1
解决办法
49
查看次数