Created
June 22, 2017 18:32
-
-
Save erm3nda/f40b91d049ada9633c6a8b3d331bae42 to your computer and use it in GitHub Desktop.
How to process a list in batches of pairwise using iter() and next()
This file contains 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/local/env/ python | |
# -*- coding:utf-8 -*- | |
import sys | |
import time | |
from threading import Thread, current_thread | |
#from queue import Queue | |
lista = [1,3,6,4,3] | |
def main(): | |
iterlist = iter(lista) | |
for i in iterlist: | |
print("Processing thread with wait of: " + str(i)) | |
t = Thread(target=thread_function, args=(i,)) | |
#t.daemon = True | |
t.start() | |
next(iterlist) | |
print("Processing thread with wait of: " + str(i)) | |
t = Thread(target=thread_function, args=(i,)) | |
#t.daemon = True | |
t.start() | |
time.sleep(2) | |
def thread_function(item): | |
if item: | |
print("Running " + current_thread().name) | |
if len(sys.argv) > 1 and sys.argv[1] in ["--wait", "-W"]: | |
print("wait " + str(item)) | |
time.sleep(item) | |
print(current_thread().name + "finished") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment