-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_same_file.py
54 lines (44 loc) · 1.23 KB
/
write_same_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import division
import os
import sys
import string
import random
import time
from binascii import hexlify
import multiprocessing as mp
def _reseed_random():
if 'random' not in sys.modules:
return
import random
# If os.urandom is available, this method does the same thing as
# random.seed (at least as of python 2.6). If os.urandom is not
# available, we mix in the pid in addition to a timestamp.
try:
seed = long(hexlify(os.urandom(16)), 16)
except NotImplementedError:
seed = int(time.time() * 1000) ^ os.getpid()
random.seed(seed)
def child_init(f, word, lock):
_reseed_random()
s = word * 5000
while True:
# with lock:
# f.write(s)
f.write(s)
f.flush()
time.sleep(int(random.uniform(0.1, 0.5) * 100) / 100)
def main():
words = string.ascii_lowercase
open("same_file.txt", "w").close()
f = open("same_file.txt", "a", 0)
lock = mp.Lock()
for i in xrange(24):
child_pid = os.fork()
if not child_pid:
# f = open("same_file.txt", "a")
child_init(f, words[i], lock)
return
os.wait()
sys.exit()
if __name__ == "__main__":
main()