init
This commit is contained in:
Executable
+26
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sqlite3
|
||||
import sys, os
|
||||
import numpy as np
|
||||
|
||||
output_file = 'points.db'
|
||||
|
||||
if os.path.exists(output_file):
|
||||
print "found old db, erasing"
|
||||
os.unlink(output_file)
|
||||
|
||||
create = 'CREATE TABLE points (point_id integer primary key, x float, y float)'
|
||||
|
||||
sqconn = sqlite3.connect(output_file)
|
||||
sqc = sqconn.cursor()
|
||||
sqc.execute(create)
|
||||
|
||||
for i in xrange(int(1e6)):
|
||||
rx = np.random.rand() * 2 - 1
|
||||
ry = np.random.rand() * 2 - 1
|
||||
sqc.execute('INSERT INTO points VALUES (NULL, ?, ?)', (rx, ry))
|
||||
|
||||
|
||||
sqconn.commit()
|
||||
sqconn.close()
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
def work():
|
||||
j = 0.0
|
||||
for i in xrange(10000000):
|
||||
j = j + j/2.0
|
||||
|
||||
|
||||
|
||||
pid = os.fork()
|
||||
if pid != 0:
|
||||
print "i spawned %d and am working" % pid
|
||||
work()
|
||||
else:
|
||||
print "i am spawn, and am working"
|
||||
work()
|
||||
os._exit(0)
|
||||
|
||||
print "parent done"
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from multiprocessing import Pool
|
||||
|
||||
def f(x):
|
||||
return x*x
|
||||
|
||||
if __name__ == '__main__':
|
||||
pool = Pool(processes = 4)
|
||||
result = pool.apply_async(f, (10,))
|
||||
print result.get()
|
||||
print pool.map(f, range(10))
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from multiprocessing import Process, Lock
|
||||
|
||||
def f(l, i, d):
|
||||
d['a'] = 'shutup'
|
||||
print 'hello world', i, d
|
||||
j = 0.0
|
||||
for i in xrange(1000000):
|
||||
j = j + j/2.0
|
||||
|
||||
if __name__ == '__main__':
|
||||
lock = Lock()
|
||||
|
||||
d = {'a': 1, 'b': 2}
|
||||
|
||||
ps = []
|
||||
for num in range(2):
|
||||
ps.append(Process(target=f, args=(lock, num, d)))
|
||||
|
||||
for p in ps: p.start()
|
||||
for p in ps: p.join()
|
||||
|
||||
print d
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import thread, time
|
||||
|
||||
def work():
|
||||
j = 0.0
|
||||
for i in xrange(10000000):
|
||||
j = j + j/2.0
|
||||
|
||||
def child(tid):
|
||||
print "%d start" % tid
|
||||
work()
|
||||
print "%d stop" % tid
|
||||
|
||||
def parent():
|
||||
i = 0
|
||||
while True:
|
||||
i += 1
|
||||
thread.start_new(child, (i,))
|
||||
if raw_input() == 'q': break
|
||||
|
||||
parent()
|
||||
Reference in New Issue
Block a user