Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Threading example
- import time, thread
- def myfunction(string,sleeptime,lock,*args):
- while 1:
- lock.acquire()
- time.sleep(sleeptime)
- lock.release()
- time.sleep(sleeptime)
- if __name__=="__main__":
- lock=thread.allocate_lock()
- thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
- thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
- python threading_example.py
- python myscript.py
- if __name__ == "__main__":
- ...
- # file one.py
- def func():
- print("func() in one.py")
- print("top-level in one.py")
- if __name__ == "__main__":
- print("one.py is being run directly")
- else:
- print("one.py is being imported into another module")
- # file two.py
- import one
- print("top-level in two.py")
- one.func()
- if __name__ == "__main__":
- print("two.py is being run directly")
- else:
- print("two.py is being imported into another module")
- python one.py
- top-level in one.py
- one.py is being run directly
- python two.py
- top-level in one.py
- one.py is being imported into another module
- top-level in two.py
- func() in one.py
- two.py is being run directly
- def printStuff(*args):
- for arg in args:
- print arg
- printStuff(1, 2, "Hello World")
- 1
- 2
- Hello World
- args = [4, 5, "Goodbye"]
- printStuff(2, 3, *args)
- 2
- 3
- 4
- 5
- Goodbye
- # a.py
- import b
- # b.py
- print "Hello World from %s!" % __name__
- if __name__ == '__main__':
- print "Hello World from %s!" % __name__
- $ python a.py
- Hello World from b!
- $ python b.py
- Hello World from __main__!
- Hello World from __main__!
- def on_the_menu(arg, *args, **kwargs):
- print arg
- print args
- print kwargs
- >>> on_the_menu(5, "spam", "eggs", "ham", sausage="and spam", spam="plenty")
- 5
- ('spam', 'eggs', 'ham')
- {'sausage': 'and spam', 'spam': 'plenty'}
Add Comment
Please, Sign In to add comment