Created
July 4, 2016 02:22
-
-
Save momijiame/6ceab6f4647286603dd9f3c3a80cc5c7 to your computer and use it in GitHub Desktop.
スレッドを起動するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from datetime import datetime | |
import threading | |
import time | |
def spawn(function, *args, **kwargs): | |
thread = threading.Thread(target=function, args=args, kwargs=kwargs) | |
thread.start() | |
return thread | |
def print_date(sleep=1): | |
current_thread = threading.current_thread() | |
while True: | |
print('{}: {}'.format(current_thread.name, datetime.now())) | |
time.sleep(sleep) | |
def main(): | |
t1 = spawn(print_date, sleep=2) | |
t2 = spawn(print_date, sleep=3) | |
t1.join() | |
t2.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment