Skip to content

Instantly share code, notes, and snippets.

View sanfx's full-sized avatar
🎯
Focusing

Sanjeev Kumar sanfx

🎯
Focusing
  • Northampton
  • 01:56 (UTC)
View GitHub Profile
@sanfx
sanfx / fileDownloader.py
Created June 14, 2014 15:41
Download multiple files parallel with multiple threads for each download.
import urllib2
import os
import sys
import time
import threading
urls = ["http://broadcast.lds.org/churchmusic/MP3/1/2/nowords/271.mp3",
"http://s1.fans.ge/mp3/201109/08/John_Legend_So_High_Remix(fans_ge).mp3",
"http://megaboon.com/common/preview/track/786203.mp3"]
@sanfx
sanfx / daemon_non_daemon.py
Created June 16, 2014 04:32
A daemon thread is ended when it's parent thread has ended, a non-daemon thread will keep the parent thread alive until the non-daemon is finished.
import threading
import time
import logging
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
def daemon():
logging.debug('Starting')
@sanfx
sanfx / emitSignal_1.py
Created July 4, 2014 17:31
This example shows how to emit signal from a function at module level , signal in turn is connected to SLOT in the class that can perform some action.
from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtGui import QWidget, QApplication
from PyQt4 import QtCore
import sys
_qObject = QtCore.QObject()
class SomeOtherClass(QWidget):
@sanfx
sanfx / emitSignal_2.py
Created July 4, 2014 17:35
How to emit signal from one class and connect it to SLOT in a different class.
from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtGui import QWidget, QApplication
import sys
class SomeClass(QWidget):
# Declare a new signal - passes Exception
has_error = pyqtSignal(Exception)
def __init__(self, parent=None):
super(SomeClass, self).__init__(parent)
"""
Find maximum memory consumed by a process specified at the command-line.
Usage:
$ python memory-usage-wait3.py cmd arg1 arg2
"""
import os
import sys
import threading
import threading
def f(arg):
global running
running += 1
print("Spawned a thread. running=%s, arg=%s" % (running, arg))
for i in range(100000):
pass
running -= 1
print("Done")
from multiprocessing import Pool
def calculate(number):
return number
if __name__ == '__main__':
pool = Pool()
result = pool.map(calculate, range(4))
from concurrent.futures import ProcessPoolExecutor
def calculate(number):
return number
with ProcessPoolExecutor() as executor:
result = executor.map(calculate, range(4))
You can write the threaded example as:
import concurrent.futures
import itertools
import random
def generate_random(count):
return [random.random() for _ in range(count)]
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
@sanfx
sanfx / gist:3280d9c731e07f0b5614
Created January 9, 2015 11:57
Nuke NoOp node with expression to sample pixel and x, y coordinates
set cut_paste_input [stack 0]
version 8.0 v6
push $cut_paste_input
NoOp {
name NoOp2
selected true
xpos 824
ypos -172
addUserKnob {20 User}
addUserKnob {12 pixelCoordinate}