Python中文
首页
教程
问答
标签
搜索
登录
注册
“ctrl+c”不能在windows上使用此python代码
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>在我的mac中,<code>ctrl + c</code>与此python代码配合得非常好。因此,脚本退出时出现KeyboardInterrupt异常</p> <p>但是在Windows10中,<code>ctrl + c</code>根本不起作用。因此,脚本将永远运行</p> <p>我不知道是什么问题</p> <p>这是我的密码:</p> <pre class="lang-py prettyprint-override"><code>import time import threading def fast_scrap(): pass def slow_scrap_thread(keyword_list: list): sleep_time = 2 for keyword in keyword_list: print(keyword) print("sleep " + str(sleep_time) + "secs") time.sleep(sleep_time) def slow_scrap(): rounds = 0 while True: keyword_list = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'] keyword_lists = list() threads = list() for i in range(len(keyword_list) // 3 + 1): keyword_lists.append(keyword_list[i * 3:i * 3 + 3]) for keyword_list in keyword_lists: thread = threading.Thread(target=slow_scrap_thread, args=(keyword_list, )) # thread.daemon = True thread.start() threads.append(thread) for thread in threads: thread.join() print("rounds: " + str(rounds)) time.sleep(3) rounds += 1 def main(): thread0 = threading.Thread(target=fast_scrap) # thread0.daemon = True thread0.start() thread1 = threading.Thread(target=slow_scrap) # thread1.daemon = True thread1.start() if __name__ == "__main__": main() </code></pre> <p>我认为我的代码中的线程有问题,但不确定是什么问题</p> <p>--------------编辑------------------</p> <p>抱歉,这里有更简单的代码:</p> <pre class="lang-py prettyprint-override"><code>import time import threading def test_func(): rounds = 0 while True: print("test_func") print("rounds: " + str(rounds)) time.sleep(3) rounds += 1 def main(): thread0 = threading.Thread(target=test_func) # thread0.daemon = True thread0.start() if __name__ == "__main__": main() </code></pre> <p>我使用conda,python3.9.7在{<cd3>}中运行这段代码</p> <p>我认为“线程中的线程”会产生问题,但问题似乎就在线程中</p> <p>对不起,这个问题只在我的环境中出现</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>问题可能是,当您尝试在线程上使用键盘中断时,线程被挂起。尝试在生成新线程的地方捕获<code>KeyboardInterrupt</code><a href="https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt" rel="nofollow noreferrer">exception</a>,并在那里加入它们</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
2 回答
如何添加表示整数的擦边字符串?
2 回答
如何添加要在Bokeh中使用的新font.ttf文件?
6 回答
如何添加要显示的矩阵XY轴编号和XY轴
8 回答
如何添加计数?
4 回答
如何添加计数器函数?
9 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
4 回答
如何添加计数器来跟踪while循环中的月份和年份?
8 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
10 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
7 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
4 回答
如何添加评论拉梅尔亚姆
9 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
10 回答
如何添加谷歌地点自动完成到Flask?
7 回答
如何添加超时、python discord bot
1 回答
如何添加超过1dp的检查
8 回答
如何添加距离方法
3 回答
如何添加跟随游戏的敌人精灵
2 回答
如何添加路径以便python可以找到程序?
5 回答
如何添加身份验证/安全性以使用happybase访问HBase?
2 回答