Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Created June 22, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erm3nda/f40b91d049ada9633c6a8b3d331bae42 to your computer and use it in GitHub Desktop.
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()
#!/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