Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

streaming_log.py 2.6 KB

You have to be logged in to leave a comment. Sign In
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  1. import datetime
  2. import io
  3. import tempfile
  4. import time
  5. import sys
  6. from requests import Session
  7. from wandb import Api
  8. import threading
  9. import traceback
  10. import logging
  11. import re
  12. import signal
  13. import os
  14. import logging
  15. import six
  16. logger = logging.getLogger(__name__)
  17. class LineBuffer(object):
  18. """Streaming string parser that extracts lines."""
  19. def __init__(self):
  20. self._buf = []
  21. self._line_end_re = re.compile('[\r\n]')
  22. def add_string(self, string):
  23. """Process a string.
  24. Args:
  25. string: Any string
  26. Returns:
  27. list of found lines, remainder will be buffered.
  28. """
  29. lines = []
  30. while string:
  31. match = self._line_end_re.search(string)
  32. if match is None:
  33. self._buf.append(string)
  34. break
  35. else:
  36. line_end_pos = match.start()
  37. lines.append(''.join(self._buf) + string[:line_end_pos + 1])
  38. string = string[line_end_pos + 1:]
  39. self._buf = []
  40. return lines
  41. class TextStreamPusher(object):
  42. """Pushes a stream of text, line by line, to wandb."""
  43. def __init__(self, fsapi, filename, line_prepend='', prepend_timestamp=False):
  44. """Conctructor.
  45. Args:
  46. fsapi: api.FileStreamApi instance
  47. filename: Name of the file this stream is pushed to.
  48. line_prepend: string to prepend to every line for this stream.
  49. prepend_timestamp: If true a timestamp will be prepended to each line
  50. (after line_prepend).
  51. """
  52. self._fsapi = fsapi
  53. self._filename = filename
  54. if line_prepend:
  55. line_prepend += ' '
  56. self._line_prepend = line_prepend
  57. self._prepend_timestamp = prepend_timestamp
  58. self._line_buffer = LineBuffer()
  59. def write(self, message, cur_time=None):
  60. """Write some text to the pusher.
  61. Args:
  62. message: a string to push for this file.
  63. cur_time: used for unit testing. override line timestamp.
  64. """
  65. if cur_time is None:
  66. cur_time = time.time()
  67. lines = self._line_buffer.add_string(message)
  68. for line in lines:
  69. timestamp = ''
  70. if self._prepend_timestamp:
  71. timestamp = datetime.datetime.utcfromtimestamp(
  72. cur_time).isoformat() + ' '
  73. line = '%s%s%s' % (self._line_prepend, timestamp, line)
  74. self._fsapi.push(self._filename, line)
  75. def close(self):
  76. """Close the file."""
  77. # Force a final line to clear whatever might be in the buffer.
  78. self.write('\n')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...