1

I have two incoming streaming, both of them are big. can I flush the first data, let say from the file's position 1 to 512 and the other from 513 to 1023? Note that that both streams are coming parallelly.

thanks!

prgbenz
  • 1,129
  • 4
  • 13
  • 27

2 Answers2

1

Not sure exactly what you mean by parallel, but you can write to two different file objects that have the same file open. I assume it doesn't matter if you are using threads or an event loop.

>>> f1 = open("/tmp/foo", "a")
>>> f2 = open("/tmp/foo", "a")
>>> f1.write("a\n")
>>> f2.write("b\n")
>>> f1.close()
>>> f2.close()
>>> print open("/tmp/foo").read()
a
b
Ben
  • 2,422
  • 2
  • 16
  • 23
0

Sure, you can do it. Are you guaranteed correct results? No. Use locks or mmap as suggested by Ignacio.

Here's a good discussion too: Python multiple threads accessing same file

Community
  • 1
  • 1
Sid
  • 7,511
  • 2
  • 28
  • 41