Skip to content

Instantly share code, notes, and snippets.

@yiidtw
Created September 9, 2018 14:35
Show Gist options
  • Save yiidtw/12820039f3a25ba402ef101eff669521 to your computer and use it in GitHub Desktop.
Save yiidtw/12820039f3a25ba402ef101eff669521 to your computer and use it in GitHub Desktop.
generate CPU loading with 5 sec and close it in python 2.7
# -*- coding: utf-8 -*-
import threading, sys, time
from math import pi
def workload(stop_event, arg):
# while not stop_event.wait(1):
# print ("working on %s" % arg)
while not stop_event.is_set():
pi * pi
print("Stopping as you wish.")
def main():
t_stop_event = threading.Event()
t = threading.Thread(target=workload, args=(t_stop_event, "some argument"))
t.start()
time.sleep(5)
t_stop_event.set()
t.join()
if __name__ == "__main__":
main()
# Ref
# https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment