Skip to content

Instantly share code, notes, and snippets.

@momijiame
Created July 4, 2016 02:22
Show Gist options
  • Save momijiame/6ceab6f4647286603dd9f3c3a80cc5c7 to your computer and use it in GitHub Desktop.
Save momijiame/6ceab6f4647286603dd9f3c3a80cc5c7 to your computer and use it in GitHub Desktop.
スレッドを起動するサンプル
#!/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