0

I have two android devices connected to my Linux machine and I need to be able to take a screenshot on both at the exact same time. How can I achieve this in python? This is what I have so far:

import time, datetime
from threading import Thread
from Queue import Queue

def get_screenshot(deviceid):
    print deviceid
    print datetime.datetime.now()
    # Logic to get screenshot goes here

deviceids = ["blabla1", "blabla2"]

q = Queue()

for d in deviceids:
    t = Thread(target=get_screenshot, args=(d,))
    t.setDaemon(True)
    t.start()

for d in deviceids:
    q.put(d)

q.join()

The output looks like this:

blabla1
2016-10-02 12:55:17.146964
blabla2
2016-10-02 12:55:17.147141

The problem is: 1. Is the approach correct? 2. Why the difference of 1ms? Can that be avoided? 3. This program does not exit. I need to Ctrl+Z out of it.

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
slimlambda
  • 153
  • 1
  • 4

1 Answers1

0
import datetime
from threading import Thread

def get_screenshot(deviceid):
    print("%s %s" %(deviceid, datetime.datetime.now()))

deviceids = ["blabla1", "blabla2"]

for d in deviceids:
    t = Thread(target=get_screenshot, args=(d,))
    t.start()
  1. You don't need a queue.
  2. 0.2 ms wasted on thread creation and starting.
  3. It's because you put strings into the Queue and than join that queue - the queue 'waits for strings' and hangs. Do you really need to join your threads? You may write your screenshots to files in the thread function. Python program automatically closes when all it's threads are finished.